Results 1 to 4 of 4
  1. #1
    Perserkatta is offline Member
    Join Date
    Apr 2014
    Location
    Sweden
    Posts
    2
    Rep Power
    0

    Default NullPointerException, help?

    I guess this is a simple quest, but I'm new to this, so I don't really know what's wrong and it would be nice if someone could help me.
    While I try to run the script I got this message: "Exception in thread 'main' java.lang.NullPointerException at kapitel2ovning4.main (kapitel2ovning4.java:16)"


    Java Code:
    import javax.swing.*;
    import java.util.*;
    public class kapitel2ovning4 {
    	public static void main(String[] args) {
    	
    	Knut a = new Knut();
    	a.rekordHojdhopp = 2;
    	a.rekordLangdhopp = 4;
    	a.rekordStavhopp = 5;
    	a.rekordAttahundraMeter = 104.52;
    	a.rekordHundraMeter = 23.12;
    	a.rekordBagskytte = 125.12;
    	
    	Knut b[] = new Knut[3];
    	
    	b[0].rekordHojdhopp = 3;  (this is line 16)
    	b[0].rekordLangdhopp = 5;
    	b[0].rekordStavhopp = 7;
    	b[0].rekordAttahundraMeter = 114.22;
    	b[0].rekordHundraMeter = 21.02;
    	b[0].rekordBagskytte = 115.15;
    	
    	b[1].rekordHojdhopp = 2;
    	b[1].rekordLangdhopp = 4;
    	b[1].rekordStavhopp = 6;
    	b[1].rekordAttahundraMeter = 104.52;
    	b[1].rekordHundraMeter = 24.52;
    	b[1].rekordBagskytte = 120.15;
    	
    	b[2].rekordHojdhopp = 3;
    	b[2].rekordLangdhopp = 3;
    	b[2].rekordStavhopp = 3;
    	b[2].rekordAttahundraMeter = 109.58;
    	b[2].rekordHundraMeter = 24.16;
    	b[2].rekordBagskytte = 117.08;
    	
    	
    	class Knut {
    	public int rekordHojdhopp;
    	public int rekordLangdhopp;
    	public int rekordStavhopp;
    	public double rekordAttahundraMeter;
    	public double rekordHundraMeter;
    	public double rekordBagskytte;
    	}
    	
    }
    }
    Last edited by Perserkatta; 04-15-2014 at 12:48 PM.

  2. #2
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,925
    Rep Power
    19

    Default Re: NullPointerException, help?

    When posting code please wrap it in [code] tags [/code] so it retains its formatting.

    Java Code:
    Knut b[] = new Knut[3];
    This line only creates the array. Each element in the array is null at this point.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    Perserkatta is offline Member
    Join Date
    Apr 2014
    Location
    Sweden
    Posts
    2
    Rep Power
    0

    Default Re: NullPointerException, help?

    Okay, and that is wrong? How am I supposed to do then?
    Last edited by Perserkatta; 04-15-2014 at 12:47 PM.

  4. #4
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    926
    Rep Power
    2

    Default Re: NullPointerException, help?

    Have you tried to fill each element of the array with an actual object?
    Java Code:
    b[0] = new Knut();
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

Similar Threads

  1. Need help with nullpointerexception
    By andrewmills in forum New To Java
    Replies: 7
    Last Post: 12-08-2012, 01:35 PM
  2. Why am I getting a NullPointerException?
    By atac57 in forum New To Java
    Replies: 7
    Last Post: 07-28-2012, 08:40 AM
  3. NullPointerException
    By Stormageddon in forum New To Java
    Replies: 3
    Last Post: 07-27-2012, 02:52 AM
  4. NullPointerException
    By Eleeist in forum New To Java
    Replies: 3
    Last Post: 02-04-2012, 09:08 PM
  5. NullPointerException
    By JohnST in forum New To Java
    Replies: 13
    Last Post: 01-06-2010, 12:37 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
  •