Novo

Forum kao aplikacija na telefonu

Dodajte forum na početni ekran i koristite ga kao aplikaciju, sa menijem na dnu ekrana.

  • 9 odgovora
  • 4.756 pregleda

Najnovija ↓

try/catch ispred konstruktora? 31.05.2008. u 17:20 · pre 224 meseca #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 20:09 · pre 224 meseca #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 20:38 · pre 224 meseca #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 20:39 · pre 224 meseca #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 22:15 · pre 224 meseca #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 22:21 · pre 224 meseca #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 23:10 · pre 224 meseca #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? 01.06.2008. u 00:02 · pre 224 meseca #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? 01.06.2008. u 00:10 · pre 224 meseca #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