Navigacija
Lista poslednjih: 16, 32, 64, 128 poruka.

try/catch ispred konstruktora?

[es] :: Java :: try/catch ispred konstruktora?

[ Pregleda: 3301 | Odgovora: 9 ] > FB > Twit

Postavi temu Odgovori

Autor

Pretraga teme: Traži
Markiranje Štampanje RSS

Ha-Nocri

Član broj: 45144
Poruke: 1192
79.101.141.*



+10 Profil

icon try/catch ispred konstruktora?31.05.2008. u 16:20 - pre 193 meseci
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
i5-750 @4GHz with CM Hyper 212+
GTX 580 Lightning XE 3GB
Corsair HX750
CM Sniper
 
Odgovor na temu

anon156554

Član broj: 156554
Poruke: 100
*.vektor.net.



Profil

icon Re: try/catch ispred konstruktora?31.05.2008. u 19:09 - pre 193 meseci

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?
 
Odgovor na temu

Mister_rap
SE at Viacom

Član broj: 8822
Poruke: 2540
77.46.241.*

Jabber: mister_rap@jabber.com


+21 Profil

icon Re: try/catch ispred konstruktora?31.05.2008. u 19:35 - pre 193 meseci
A sto ne napravis posebnu metodu za to!?
 
Odgovor na temu

Ha-Nocri

Član broj: 45144
Poruke: 1192
79.101.141.*



+10 Profil

icon Re: try/catch ispred konstruktora?31.05.2008. u 19:38 - pre 193 meseci
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...
i5-750 @4GHz with CM Hyper 212+
GTX 580 Lightning XE 3GB
Corsair HX750
CM Sniper
 
Odgovor na temu

Ha-Nocri

Član broj: 45144
Poruke: 1192
79.101.141.*



+10 Profil

icon Re: try/catch ispred konstruktora?31.05.2008. u 19:39 - pre 193 meseci
@Mister_rap

Kako da napravim metodu? Potrebno mi je da DEGREE's budu static...
i5-750 @4GHz with CM Hyper 212+
GTX 580 Lightning XE 3GB
Corsair HX750
CM Sniper
 
Odgovor na temu

anon156554

Član broj: 156554
Poruke: 100
*.vektor.net.



Profil

icon Re: try/catch ispred konstruktora?31.05.2008. u 21:15 - pre 193 meseci
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?
 
Odgovor na temu

Ha-Nocri

Član broj: 45144
Poruke: 1192
79.101.141.*



+10 Profil

icon Re: try/catch ispred konstruktora?31.05.2008. u 21:21 - pre 193 meseci
Probao sam i to, nece. Opet je 2 puta dodeljena vrednost, a ako je final moze samo jednom kontam...
i5-750 @4GHz with CM Hyper 212+
GTX 580 Lightning XE 3GB
Corsair HX750
CM Sniper
 
Odgovor na temu

samilen
Saša Milenković
Beograd

Član broj: 11606
Poruke: 106
89.216.81.*



Profil

icon Re: try/catch ispred konstruktora?31.05.2008. u 22:10 - pre 193 meseci
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...
}
 
Odgovor na temu

Ha-Nocri

Član broj: 45144
Poruke: 1192
79.101.141.*



+10 Profil

icon Re: try/catch ispred konstruktora?31.05.2008. u 23:02 - pre 193 meseci
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.
i5-750 @4GHz with CM Hyper 212+
GTX 580 Lightning XE 3GB
Corsair HX750
CM Sniper
 
Odgovor na temu

Ha-Nocri

Član broj: 45144
Poruke: 1192
79.101.141.*



+10 Profil

icon Re: try/catch ispred konstruktora?31.05.2008. u 23:10 - pre 193 meseci
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 :-)
i5-750 @4GHz with CM Hyper 212+
GTX 580 Lightning XE 3GB
Corsair HX750
CM Sniper
 
Odgovor na temu

[es] :: Java :: try/catch ispred konstruktora?

[ Pregleda: 3301 | Odgovora: 9 ] > FB > Twit

Postavi temu Odgovori

Navigacija
Lista poslednjih: 16, 32, 64, 128 poruka.