Results 1 to 7 of 7
  1. Neutron Male
    IGN: Yugidude3536
    Server: Bera
    Level: 177
    Job: Hero
    Guild: Prospect
    Alliance: ComeAtUsBro
    Michigan

    Default Java - Keep getting NullPointerExemption


    So as im working on teaching myself java, but im stuck on a problem and i was hoping someone could show me what im missing.

    Code:
    JLabel[][] SkillLabels
    int skill = 1;
    		while (skill < 4) {
    
    			SkillLabels[skill][1] = new JLabel(
    					SkillDatabase.Skills[1].getName());
    			SkillLabels[skill][2] = new JLabel("Current Level:");
    			SkillLabels[skill][3] = new JLabel("Current Stats:");
    			SkillLabels[skill][4] = new JLabel(
    					SkillDatabase.Skills[1].getDesc());
    
    			LevelSkill[skill] = new JButton("Level Skill");
    			LevelSkill[skill].addActionListener(this);
    			skill++;
    		}
    1. Im trying to make a GUI, and what you see above are the various JLabels in it. The while loop (should) loop 3 times, once for each of the three skills my game has. The SkillDatabase.Skills stuff is where i keep the information on each skill (what it does, what its called, what its current level is, etc)
    2. When i run this code, i get a NullPointerExemption, and it tells me the first line in the loop is the problem. I have tried re-aranging the loop, and it always errors on the 1st one.
    3. In case the point of the loop isnt clear, I have a 2D Array of JLabels (the first brackets represents skill#, 2nd represents one of it's four JLabels), and i am trying to assign all of the Values to it in an easy way.

    Well, thats it. I beleive i have attached all the code that caused the problem, but if something else is needed just lemme know.

    Thanks!

  2. Orbital Bee Cannon
    IGN: SaptaZapta
    Server: Kradia
    Level: 275
    Job: Hero
    Guild: Matriarchy
    Alliance: Peaceful

    Default Re: Java - Keep getting NullPointerExemption


    You need to allocate memory for SkillLabels before you can access its cells.

  3. Default Re: Java - Keep getting NullPointerExemption


    I wish this was StackOverflow so I can edit your indentation, it's.. pretty bad.

    Code:
    JLabel[][] SkillLabels
    int skill = 1;
    while (skill < 4) {
    
        SkillLabels[skill][1] = new JLabel(SkillDatabase.Skills[1].getName());
        SkillLabels[skill][2] = new JLabel("Current Level:");
        SkillLabels[skill][3] = new JLabel("Current Stats:");
        SkillLabels[skill][4] = new JLabel(SkillDatabase.Skills[1].getDesc());
    
        LevelSkill[skill] = new JButton("Level Skill");
        LevelSkill[skill].addActionListener(this);
        skill++;
    }
    Anyway, a null pointer exception is a type of exception that happens when you have a reference type and do not initialize it, ergo it is "null".

    Consider the following lines:
    Code:
    Integer x; //null
    x = new Integer(42); //no longer null
    So a null pointer exception happens when you try to dereference that null value.

    Consider the following function:
    Code:
    public void myFunction(Integer x) {
        //do something with x here while x is null
        //NullPointerException thrown
    }
    Edit: Sapta has a point actually, I didn't notice that you didn't initialize your original SkillLabels. So there you go.

  4. Default Re: Java - Keep getting NullPointerExemption


    When you create objects in Java, they are not immediately initialized. The statement

    Code:
    JLabel[][] SkillLabels;
    does not create an 2d array of JLabels. All it does is make a pointer (a variable that stores, or "points to", a certain memory address) to one and point it at nothing. So when you try to use that reference, you get a NullPointerException, because it does not exist. In order to actually create your array, you have to ask the JVM to allocate some memory for it. You do this with the new keyword. To initialize an array of JLabels with size skill x 4, you'd write:

    Code:
    JLabel[][] SkillLabels = new JLabel[skill][4];
    You'd also have to change your array indices to 0-3.

  5. Neutron Male
    IGN: Yugidude3536
    Server: Bera
    Level: 177
    Job: Hero
    Guild: Prospect
    Alliance: ComeAtUsBro
    Michigan

    Default Re: Java - Keep getting NullPointerExemption


    my bad. I looked up more information on arrays, and I beleive I was able to allocate memory correctly, but im still getting the same error. did I do it wrong?

    Code:
    int skill = 1;
    JLabel[][] SkillLabels;
    SkillLabels = new JLabel[10][10];
    while (skill < 4) {
    	SkillLabels[skill][1] = new JLabel(SkillDatabase.Skills[1].getName());
    	SkillLabels[skill][2] = new JLabel("Current Level:");
    	SkillLabels[skill][3] = new JLabel("Current Stats:");
    	SkillLabels[skill][4] = new JLabel(SkillDatabase.Skills[1].getDesc());
    	LevelSkill[skill] = new JButton("Level Skill");
    	LevelSkill[skill].addActionListener(this);
    	skill++;
    		}

  6. Default Re: Java - Keep getting NullPointerExemption


    Is LevelSkill initialized?

  7. Neutron Male
    IGN: Yugidude3536
    Server: Bera
    Level: 177
    Job: Hero
    Guild: Prospect
    Alliance: ComeAtUsBro
    Michigan

    Default Re: Java - Keep getting NullPointerExemption


    Yes. I did not included that in my code because it crashes before it even gets to that point. It crashes at the 1st line of the loop

    EDIT: I noticed my problem. After I added in that line that @Locked pointed out I was missing, the crash log moved down one line from the "SkillLabels[skill][1] = new JLabel(" line to the "SkillDatabase.Skills[1].getName());"

    I had not relized it at the time, but I do not assign a name value to skill one until about 50 lines later. I pushed that segment to the top and boom, works fine. Thanks alot to everyone who helped me through my newby-struggles

  8.  

Bookmarks

Posting Permissions

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