Results 1 to 5 of 5
- 11-14-2012, 12:58 PM #1
Member
- Join Date
- Nov 2012
- Posts
- 5
- Rep Power
- 0
Exception in thread "main" java.lang.NullPointerException
Hello, I really hopeless and I feel like this is my last chance to fix that, before I will have to send this to my teacher.
I'm getting this error:
Exception in thread "main" java.lang.NullPointerException
at ui.PanelBatohu.<init>(PanelBatohu.java:33)
at ui.Gui.init(Gui.java:107)
at ui.Gui.<init>(Gui.java:68)
at main.Start.main(Start.java:25)
Legend: Doing graphic interface for this one little java game.
Batoh= Bag(container in game)
zaregistruj pozorovatele(register observer)
first error line is:
[30] public PanelBatohu(Batoh batoh) {
[31] super();
[32] this.batoh = batoh;
[33] batoh.zaregistrujPozorovatele(this);
[34] this.aktualizuj(batoh);
[35]
[36] this.setBorder(BorderFactory.createTitledBorder("V ěci v batohu:"));
[37] this.setVisible(true);
}
second error line is:
[107]panelBatohu = new PanelBatohu(hra.getBatoh());
[108] hlavniOknoFrame.add(panelBatohu, BorderLayout.WEST);[LIST=1]
third error line:
[65]public Gui(Hra hra) {
[66] this.hra = hra;
[67] initMenu();
[68] init();
fourth error line:
[23]public static void main(String[] args) {
[24] if (args.length == 0) {
[25] Gui grafika = new Gui(new Hra());
- 11-14-2012, 01:34 PM #2
Senior Member
- Join Date
- Dec 2008
- Location
- Kolkata
- Posts
- 280
- Rep Power
- 5
Re: Exception in thread "main" java.lang.NullPointerException
You get nullpointerexception when you are calling a method of an object, but the object is holding a null reference, i.e. it has not been initialized with a valid reference. So without looking at your complete code, it's to know what's going wrong.
Swastik
- 11-14-2012, 01:54 PM #3
Member
- Join Date
- Nov 2012
- Posts
- 5
- Rep Power
- 0
Re: Exception in thread "main" java.lang.NullPointerException
Hey, thanks for reaction:)
here's whole code of that class:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package ui;
import java.net.URL;
import javax.swing.*;
import logika.HerniPlan;
import logika.Batoh;
import logika.Prostor;
import util.ObserverZmenyBatohu;
/**
* Instance třídy PanelBatohu představují panel batohu.
*
* @author
*/
public class PanelBatohu extends JPanel implements ObserverZmenyBatohu {
private Batoh batoh;
private Icon logoIcon;
private String[] oddeleneVeci;
/**
* Konstruktor panelu batohu.
*
* @param inventar logika
*/
public PanelBatohu(Batoh batoh) {
super();
this.batoh = batoh;
batoh.zaregistrujPozorovatele(this);
this.aktualizuj(batoh);
this.setBorder(BorderFactory.createTitledBorder("V ěci v batohu:"));
this.setVisible(true);
}
/**
* Metoda zaregistruje pozorovatele.
*
* @param inventar logika
*/
public void nastaveniBatohu(Batoh batoh) {
this.batoh = batoh;
batoh.zaregistrujPozorovatele(this);
this.aktualizuj(batoh);
}
/**
* Metoda, ve které proběhne aktualizace pozorovatele.
*
* @param aktualniVeci logika
*/
public void aktualizuj(Batoh aktualniVeci) {
this.removeAll();
String veci = aktualniVeci.seznamVeci();
oddeleneVeci = veci.split(" ");
URL umisteniObrazku;
for (int i = 1; i < oddeleneVeci.length; i++) {
umisteniObrazku = this.getClass().getResource("/zdroje/" + oddeleneVeci[i] + ".jpg");
if (umisteniObrazku == null) {
JOptionPane.showMessageDialog(null,
"Soubor s obrázkem nebyl nalezen.",
"Chyba při načítání obrázku", JOptionPane.ERROR_MESSAGE);
} else {
logoIcon = new ImageIcon(umisteniObrazku);
JLabel obrazek = new JLabel(logoIcon);
this.add(obrazek);
}
}
}
}
- 11-14-2012, 02:11 PM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Re: Exception in thread "main" java.lang.NullPointerException
Please use [code] tags [/code] (not quote tags) when posting code.
'batoh' is null before even getting into the constructor above.
Your problem lies before that point, probably in the Hra class, since that's where you are getting a Batoh from.Please do not ask for code as refusal often offends.
- 11-14-2012, 05:12 PM #5
Similar Threads
-
Another Exception in thread "main" java.lang.NullPointerException problem
By wdh321 in forum New To JavaReplies: 6Last Post: 04-19-2012, 07:10 PM -
Got struck with this :- " Exception in thread "main" java.lang.NullPointerException"
By Vermont in forum New To JavaReplies: 5Last Post: 12-21-2011, 06:44 PM -
Exception in thread "main" java.lang.NullPointerException at LinkedList.main(Link
By kavitha_0821 in forum New To JavaReplies: 6Last Post: 07-16-2009, 03:30 PM -
Exception in thread "main" java.lang.NullPointerException at LinkedList.main(Link
By kavitha_0821 in forum New To JavaReplies: 1Last Post: 07-16-2009, 10:35 AM -
ArrayList: Exception in thread "main" java.lang.NullPointerException
By susan in forum New To JavaReplies: 1Last Post: 07-16-2007, 06:32 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks