Results 1 to 5 of 5
  1. #1
    Legaly is offline Member
    Join Date
    Nov 2012
    Posts
    5
    Rep Power
    0

    Default 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());

  2. #2
    dswastik is offline Senior Member
    Join Date
    Dec 2008
    Location
    Kolkata
    Posts
    282
    Rep Power
    6

    Default 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

  3. #3
    Legaly is offline Member
    Join Date
    Nov 2012
    Posts
    5
    Rep Power
    0

    Default 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);
    }
    }
    }
    }

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,820
    Rep Power
    19

    Default 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.

    ** This space for rent **

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Exception in thread "main" java.lang.NullPointerException

    Quote Originally Posted by dswastik View Post
    ... but the object is holding a null reference
    Variables hold references to objects. Objects don't hold references.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Replies: 6
    Last Post: 04-19-2012, 07:10 PM
  2. Replies: 5
    Last Post: 12-21-2011, 06:44 PM
  3. Replies: 6
    Last Post: 07-16-2009, 03:30 PM
  4. Replies: 1
    Last Post: 07-16-2009, 10:35 AM
  5. Replies: 1
    Last Post: 07-16-2007, 06:32 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •