• 9 odgovora
  • 4.742 pregleda
try/catch ispred konstruktora? 31.05.2008. u 16:20 · pre 223 meseci #1
Poz,

Code:
class Book {
    public static final ImageIcon DEGREE1 = new ImageIcon(Book.class.getResource("slike/degree1.png"));
    public static final ImageIcon DEGREE2 = new ImageIcon(Book.class.getResource("slike/degree2.png"));
    public static final ImageIcon DEGREE3 = new ImageIcon(Book.class.getResource("slike/degree3.png"));
    public static final ImageIcon DEGREE4 = new ImageIcon(Book.class.getResource("slike/degree4.png"));
    public static final ImageIcon DEGREE5 = new ImageIcon(Book.class.getResource("slike/degree5.png"));

    ...etc...
}


Metod getResource(fileName) baca NullPointerException ako datoteka nije pronadjena, a ja ne mogu da koristim try/catch blok ispred konstruktora izgleda. Kako da resim ovaj problem?

Hvala unapred
H
Potpis
i5-750 @4GHz with CM Hyper 212+
GTX 580 Lightning XE 3GB
Corsair HX750
CM Sniper
Re: try/catch ispred konstruktora? 31.05.2008. u 19:09 · pre 223 meseci #2

class Book {
public static final ImageIcon DEGREE1;
public static final ImageIcon DEGREE2;
public static final ImageIcon DEGREE3;
public static final ImageIcon DEGREE4;
public static final ImageIcon DEGREE5;

static{
try{
DEGREE1 = new ImageIcon(Book.class.getResource("slike/degree1.png"));
DEGREE2 = new ImageIcon(Book.class.getResource("slike/degree2.png"));
DEGREE3 = new ImageIcon(Book.class.getResource("slike/degree3.png"));
DEGREE4 = new ImageIcon(Book.class.getResource("slike/degree4.png"));
DEGREE5 = new ImageIcon(Book.class.getResource("slike/degree5.png"));
}catch(Exception e){}

}


...etc...
}

Mozda inicijalizacija u static bloku moze da zavrsi posao?
Re: try/catch ispred konstruktora? 31.05.2008. u 19:38 · pre 223 meseci #4
Ne radi. Kaze da DEGEE's mozda nisu inicijalizovali. Onda sam stavio u catch null vrednosti da prevarim kompajler:

Code:

    public static final ImageIcon DEGREE1;
    public static final ImageIcon DEGREE2;
    public static final ImageIcon DEGREE3;
    public static final ImageIcon DEGREE4;
    public static final ImageIcon DEGREE5;

    static{
        try {
            DEGREE1 = new ImageIcon(Book.class.getResource("slike/degree1.png"));
            DEGREE2 = new ImageIcon(Book.class.getResource("slike/degree2.png"));
            DEGREE3 = new ImageIcon(Book.class.getResource("slike/degree3.png"));
            DEGREE4 = new ImageIcon(Book.class.getResource("slike/degree4.png"));
            DEGREE5 = new ImageIcon(Book.class.getResource("slike/degree5.png"));
        } catch(NullPointerException ex){
            DEGREE1 = null;
            DEGREE2 = null;
            DEGREE3 = null;
            DEGREE4 = null;
            DEGREE5 = null;
        }
    }


Ovde kaze da su DEGREE's mozda 2 puta inicijalizovani. Glupost, ali ne radi...
Potpis
i5-750 @4GHz with CM Hyper 212+
GTX 580 Lightning XE 3GB
Corsair HX750
CM Sniper
Re: try/catch ispred konstruktora? 31.05.2008. u 19:39 · pre 223 meseci #5
@Mister_rap

Kako da napravim metodu? Potrebno mi je da DEGREE's budu static...
Potpis
i5-750 @4GHz with CM Hyper 212+
GTX 580 Lightning XE 3GB
Corsair HX750
CM Sniper
Re: try/catch ispred konstruktora? 31.05.2008. u 21:15 · pre 223 meseci #6
class Book {
public static final ImageIcon DEGREE1 = null;
public static final ImageIcon DEGREE2 = null;
public static final ImageIcon DEGREE3 = null;
public static final ImageIcon DEGREE4 = null;
public static final ImageIcon DEGREE5 = null;

static{
try{
DEGREE1 = new ImageIcon(Book.class.getResource("slike/degree1.png"));
DEGREE2 = new ImageIcon(Book.class.getResource("slike/degree2.png"));
DEGREE3 = new ImageIcon(Book.class.getResource("slike/degree3.png"));
DEGREE4 = new ImageIcon(Book.class.getResource("slike/degree4.png"));
DEGREE5 = new ImageIcon(Book.class.getResource("slike/degree5.png"));
}catch(Exception e){}

}


...etc...
}



A ovako?
Re: try/catch ispred konstruktora? 31.05.2008. u 21:21 · pre 223 meseci #7
Probao sam i to, nece. Opet je 2 puta dodeljena vrednost, a ako je final moze samo jednom kontam...
Potpis
i5-750 @4GHz with CM Hyper 212+
GTX 580 Lightning XE 3GB
Corsair HX750
CM Sniper
Re: try/catch ispred konstruktora? 31.05.2008. u 22:10 · pre 223 meseci #8
Probaj:
Code:

class Book {
public static final ImageIcon DEGREE1;
public static final ImageIcon DEGREE2;
public static final ImageIcon DEGREE3;
public static final ImageIcon DEGREE4;
public static final ImageIcon DEGREE5;

static{
  DEGREE1 = null;
  DEGREE2 = null;
  DEGREE3 = null;
  DEGREE4 = null;
  DEGREE5 = null;
  try{
    DEGREE1 = new ImageIcon(Book.class.getResource("slike/degree1.png"));
    DEGREE2 = new ImageIcon(Book.class.getResource("slike/degree2.png"));
    DEGREE3 = new ImageIcon(Book.class.getResource("slike/degree3.png"));
    DEGREE4 = new ImageIcon(Book.class.getResource("slike/degree4.png"));
    DEGREE5 = new ImageIcon(Book.class.getResource("slike/degree5.png"));
  }catch(Exception e){ 
  }

}


...etc...
}
Re: try/catch ispred konstruktora? 31.05.2008. u 23:02 · pre 223 meseci #9
Isto:

"The final field DEGREE1 may already have been assigned"

Isto i za DEGREE2, 3, 4 i 5. Ovo prijavljuje u try/catch bloku posto se tu po drugi put u kodu dodeljuju vrednosti.
Potpis
i5-750 @4GHz with CM Hyper 212+
GTX 580 Lightning XE 3GB
Corsair HX750
CM Sniper
Re: try/catch ispred konstruktora? 31.05.2008. u 23:10 · pre 223 meseci #10
Proradilo. Izbrisao sam final gde god je bilo i radi...

Code:

class Book {
    public static ImageIcon DEGREE1;
    public static ImageIcon DEGREE2;
    public static ImageIcon DEGREE3;
    public static ImageIcon DEGREE4;
    public static ImageIcon DEGREE5;

    static {
        try {
            DEGREE1 = new ImageIcon(Book.class.getResource("slike/degree1.png"));
            DEGREE2 = new ImageIcon(Book.class.getResource("slike/degree2.png"));
            DEGREE3 = new ImageIcon(Book.class.getResource("slike/degree3.png"));
            DEGREE4 = new ImageIcon(Book.class.getResource("slike/degree4.png"));
            DEGREE5 = new ImageIcon(Book.class.getResource("slike/degree5.png"));
        } catch(NullPointerException ex) {}
    }
}


Naucio sam da sa konstantama nema puno mudrosti. Mora biti inicijalizovana jednom, na pocetku obicno, i to je to.
Hvala svima na pomoci :-)
Potpis
i5-750 @4GHz with CM Hyper 212+
GTX 580 Lightning XE 3GB
Corsair HX750
CM Sniper
Prijavite se da biste odgovorili u ovoj temi.PrijavaRegistracija

Premeštanje teme