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

try/catch ispred konstruktora?

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

[ Pregleda: 396 | Odgovora: 9 ]

Postavi temu Odgovori

Autor

Pretraga teme: Traži
Markiranje Štampanje RSS

Ha-Nocri

Član broj: 45144
Poruke: 43
79.101.141.*



Profil

icon try/catch ispred konstruktora?31.05.2008. u 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
31.05.2008. u 16:20 

odalinjo
Beograd/Smederevo

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



Profil

icon Re: try/catch ispred konstruktora?31.05.2008. u 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?
31.05.2008. u 19:09 

Mister_rap
Aleksandar Šćepanović
Student, TQS Interactive Team.
Beograd

SuperModerator
Član broj: 8822
Poruke: 1743
77.46.241.*

Jabber: mister_rap@jabber.com
Sajt: www.spike.com


Profil

icon Re: try/catch ispred konstruktora?31.05.2008. u 19:35
A sto ne napravis posebnu metodu za to!?
Diplomat of swing with aliens at my feet
Comin' down the rampart through beam on the street...

31.05.2008. u 19:35 

Ha-Nocri

Član broj: 45144
Poruke: 43
79.101.141.*



Profil

icon Re: try/catch ispred konstruktora?31.05.2008. u 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...
31.05.2008. u 19:38 

Ha-Nocri

Član broj: 45144
Poruke: 43
79.101.141.*



Profil

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

Kako da napravim metodu? Potrebno mi je da DEGREE's budu static...
31.05.2008. u 19:39 

odalinjo
Beograd/Smederevo

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



Profil

icon Re: try/catch ispred konstruktora?31.05.2008. u 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?
31.05.2008. u 21:15 

Ha-Nocri

Član broj: 45144
Poruke: 43
79.101.141.*



Profil

icon Re: try/catch ispred konstruktora?31.05.2008. u 21:21
Probao sam i to, nece. Opet je 2 puta dodeljena vrednost, a ako je final moze samo jednom kontam...
31.05.2008. u 21:21 

samilen
Saša Milenković
Beograd

Član broj: 11606
Poruke: 95
89.216.81.*

Sajt: www.sasha.in.rs


Profil

icon Re: try/catch ispred konstruktora?31.05.2008. u 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...
}
31.05.2008. u 22:10 

Ha-Nocri

Član broj: 45144
Poruke: 43
79.101.141.*



Profil

icon Re: try/catch ispred konstruktora?31.05.2008. u 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.
31.05.2008. u 23:02 

Ha-Nocri

Član broj: 45144
Poruke: 43
79.101.141.*



Profil

icon Re: try/catch ispred konstruktora?31.05.2008. u 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 :-)
31.05.2008. u 23:10 

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

[ Pregleda: 396 | Odgovora: 9 ]

Postavi temu Odgovori

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