[ Ha-Nocri @ 31.05.2008. 16:20 ] @
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
[ anon156554 @ 31.05.2008. 19:09 ] @

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?
[ Mister_rap @ 31.05.2008. 19:35 ] @
A sto ne napravis posebnu metodu za to!?
[ Ha-Nocri @ 31.05.2008. 19:38 ] @
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...
[ Ha-Nocri @ 31.05.2008. 19:39 ] @
@Mister_rap

Kako da napravim metodu? Potrebno mi je da DEGREE's budu static...
[ anon156554 @ 31.05.2008. 21:15 ] @
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?
[ Ha-Nocri @ 31.05.2008. 21:21 ] @
Probao sam i to, nece. Opet je 2 puta dodeljena vrednost, a ako je final moze samo jednom kontam...
[ samilen @ 31.05.2008. 22:10 ] @
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...
}
[ Ha-Nocri @ 31.05.2008. 23:02 ] @
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.
[ Ha-Nocri @ 31.05.2008. 23: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 :-)