Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: Problem with array

  1. #1
    Junior Member
    Join Date
    Aug 2010
    Posts
    25
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Problem with array

    Hi,
    when i run my program, it shows this
    "Exception in thread "main" java.lang.NullPointerException"
    but my array doesn't not contain any null value, how can i check if the array is null or not, or where is the null value contain? how can i solve this?
    Thank you for helping


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,167
    My Mood
    Hungover
    Thanks
    141
    Thanked 597 Times in 512 Posts

    Default Re: Problem with array

    Post an SSCCE.

    You can just do a check against null (someObject == null), but I would bet that you have a more fundamental problem.

  3. #3
    Junior Member
    Join Date
    Aug 2010
    Posts
    25
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem with array

    This is the code
    import java.io.*;
     
    public class P {
    	public static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    	public static int [][] Graph;
    	public static int [][] Tree;
            public static tempEdge [] tedges;
            private static int tempi = 0, one, two, size;
     
            public void buildTable(String file){
                n = 5;
                tedges = new tempEdge[n*(n-1)/2];
                try{
                    BufferedReader input = new BufferedReader(new FileReader(file));
                    while(input.ready())
                    {
                       String tmp = input.readLine();
                       String[] newPoint = tmp.split(" ");
                       int p1 = Integer.parseInt(newPoint[0]);
                       int p2 = Integer.parseInt(newPoint[1]);
                       int dis = Integer.parseInt(newPoint[2]);
                       tedges[tempi] = new tempEdge(p1,p2,dis);
                       System.out.println(tedges[tempi].firstPoint());
                       System.out.println(tedges[tempi].secondPoint());
                       System.out.println(tedges[tempi].distance());
                       tempi++;
                     }
                }
                catch(IOException e)
                {
    		System.out.println("Error, file does not exist");
                }
                p();
            }
            //create a minal spanning tree
    	public static void p()
    	{
                    n = 5;
                    //set size of the array which store different things
    		Graph = new int[n+1][n+1];
    		Tree = new int[n+1][3];
     
                    while(tedges != null){
                       for(int i=0;i<=tedges.length;i++){
                            //tempEdge e = tedges[i];
                                one = tedges[i].firstPoint();
                                two = tedges[i].secondPoint();
                                size = tedges[i].distance();
                                if((one!=two)&&(one<two)) {
                                    Graph[one][two] = Graph[two][one] = size;
                                }
                        }
    	}
     
    .....
     
            public static void main (String[] args) throws IOException
            {
                   P p = new P();
                   p.buildTable("3Dtriangle.txt");
    	}
    }

    and the file "3Dtriangle.txt" contains
    1 2 50
    1 3 30
    1 4 20
    2 3 15
    2 4 45
    3 4 60

    I don't know why when the program goes to p(), then it will stops and said
    "Exception in thread "main" java.lang.NullPointerException"
    Last edited by JavaPF; October 26th, 2010 at 08:50 AM. Reason: Please use [highlight=Java] code [/highlight] tags..

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,167
    My Mood
    Hungover
    Thanks
    141
    Thanked 597 Times in 512 Posts

    Default Re: Problem with array

    Put some print statements in there, or step through it with a debugger. I'd bet that it's not actually entering all of the methods you think it is.

    Hint: What does a constructor declaration look like?

    Also, you really should follow standard naming conventions. Classes start with an uppercase letter, and methods and variables start with a lowercase letter. Using more descriptive names wouldn't hurt either.

  5. #5
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Problem with array

    String[] newPoint = tmp.split(" ");

    Why would an entire String array be set to a single String value?

    int p1 = Integer.parseInt(newPoint[0]);
    int p2 = Integer.parseInt(newPoint[1]);
    int dis = Integer.parseInt(newPoint[2]);

    You never set the value for newPoint[0], newPoint[1], and newPoint[2].

    That's likely the cause of the Null Pointer Exception.

  6. #6
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Problem with array

    Quote Originally Posted by mingming8888 View Post
    This is the code
    import java.io.*;
     
    public class P {
    	public static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    	public static int [][] Graph;
    	public static int [][] Tree;
            public static tempEdge [] tedges;
            private static int tempi = 0, one, two, size;
     
            public void buildTable(String file){
                n = 5;
                tedges = new tempEdge[n*(n-1)/2];
                try{
                    BufferedReader input = new BufferedReader(new FileReader(file));
                    while(input.ready())
                    {
                       String tmp = input.readLine();
                       String[] newPoint = tmp.split(" ");
                       int p1 = Integer.parseInt(newPoint[0]);
                       int p2 = Integer.parseInt(newPoint[1]);
                       int dis = Integer.parseInt(newPoint[2]);
                       tedges[tempi] = new tempEdge(p1,p2,dis);
                       System.out.println(tedges[tempi].firstPoint());
                       System.out.println(tedges[tempi].secondPoint());
                       System.out.println(tedges[tempi].distance());
                       tempi++;
                     }
                }
                catch(IOException e)
                {
    		System.out.println("Error, file does not exist");
                }
                p();
            }
            //create a minal spanning tree
    	public static void p()
    	{
                    n = 5;
                    //set size of the array which store different things
    		Graph = new int[n+1][n+1];
    		Tree = new int[n+1][3];
     
                    while(tedges != null){
                       for(int i=0;i<=tedges.length;i++){
                            //tempEdge e = tedges[i];
                                one = tedges[i].firstPoint();
                                two = tedges[i].secondPoint();
                                size = tedges[i].distance();
                                if((one!=two)&&(one<two)) {
                                    Graph[one][two] = Graph[two][one] = size;
                                }
                        }
    	}
     
    .....
     
            public static void main (String[] args) throws IOException
            {
                   P p = new P();
                   p.buildTable("3Dtriangle.txt");
    	}
    }

    and the file "3Dtriangle.txt" contains
    1 2 50
    1 3 30
    1 4 20
    2 3 15
    2 4 45
    3 4 60

    I don't know why when the program goes to p(), then it will stops and said
    "Exception in thread "main" java.lang.NullPointerException"
    Also, I'm not sure if it'll know the new value that you've set for tedges in buildTable when you use tedges in p.

  7. #7
    Junior Member
    Join Date
    Aug 2010
    Posts
    25
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem with array

    i have tried to run it, and i got the result below
    1
    2
    50
    1
    3
    60
    1
    4
    70
    2
    3
    80
    Exception in thread "main" java.lang.NullPointerException
    2
    4
    40
    3
    4
    60
    1
    1
    1
    1
    1
    1
    It shows that tedges[] can read from the file, but i am not sure why there is null value?

  8. #8
    Member DavidFongs's Avatar
    Join Date
    Oct 2010
    Location
    Minneapolis, MN
    Posts
    107
    Thanks
    1
    Thanked 45 Times in 41 Posts

    Default Re: Problem with array

    Please post the entire stack that is printed to the console when the exception is thrown

  9. #9
    Junior Member
    Join Date
    Aug 2010
    Posts
    25
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem with array

    u mean this??

    at triangle_projection.P.p(Prim.java:69) - for(int i=0;i<=tedges.length;i++)
    at triangle_projection.P.buildTable(Prim.java:55) - p();
    at triangle_projection.P.main(Prim.java:178) - p.buildTable("3Dtriangle.txt");

  10. #10
    Member DavidFongs's Avatar
    Join Date
    Oct 2010
    Location
    Minneapolis, MN
    Posts
    107
    Thanks
    1
    Thanked 45 Times in 41 Posts

    Default Re: Problem with array

    That is not the full stack, but it looks like it is part of it

Similar Threads

  1. [SOLVED] Array Problem
    By Cammack in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 5th, 2010, 05:11 PM
  2. [SOLVED] Array Problem
    By KevinGreen in forum Object Oriented Programming
    Replies: 2
    Last Post: January 24th, 2010, 08:07 PM
  3. array/string problem
    By RSYR in forum Collections and Generics
    Replies: 1
    Last Post: December 18th, 2009, 09:24 PM
  4. Problem with 2d array
    By Anyone in forum Collections and Generics
    Replies: 2
    Last Post: November 14th, 2009, 08:32 PM
  5. Java program for 2-D Array Maze
    By Peetah05 in forum Collections and Generics
    Replies: 11
    Last Post: May 8th, 2009, 04:30 AM