Null Pointer Exception

Writing Dijkstra problem

Page 1 of 1

10 Replies - 788 Views - Last Post: 02 December 2010 - 06:51 AM Rate Topic: -----

#1 kngofdrkns  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 82
  • Joined: 21-May 08

Null Pointer Exception

Posted 01 December 2010 - 08:49 AM

I am writing a code to get the minimum latency between servers to send email. The user will enter the number of cases , case attributes (n,m,s,t) where n is number of server(vertices), m number of cables(Edges), S source server, T destination server. Then the user will enter the description of the case (servers to be connected and the weight).
I wrote 3 classes Vertex, Edge and Main classes. in the main i get the user input and create and array of vertcies and add the vertcies then added the edges to each vertex.
2 // # of test cases
n m s t
2 1 0 1 // case #1
0 1 100
Case #1: 100
3 3 2 0 // case #2
0 1 100
0 2 200
1 2 50
Case #2: 150

The problem is that in line 108 i get an error saying null pointer exception.

import java.util.PriorityQueue;
import java.util.Scanner;

public class Main {

    static class Vertex implements Comparable<Vertex>
{
    public int name, curIndex;
    public Edge[] adjacencies;
    public double minDistance = Double.POSITIVE_INFINITY;
    public Vertex previous;
    public Vertex(int argName) { name = argName; }
    public int compareTo(Vertex other)
    {
        return Double.compare(minDistance, other.minDistance);
    }

}


static class Edge
{
    public Vertex target;
    public double weight;
    public Edge(Vertex argTarget, double argWeight)
    { target = argTarget; weight = argWeight; }
}


    public static void main(String[] args) {
        // TODO code application logic here
        int uIn,n,m,S,T,N,W,v1,v2,length;
        int uInArray []= new int [ 4 ];
        int[] uInArray1 = new int [ 3 ];
        Scanner s = new Scanner(System.in);
        Vertex ver,u,v;
        v = new Vertex(100);
        u =  new Vertex(101);
        Edge ed = null;
        Vertex[] verArray;
        PriorityQueue<Vertex> pq;

        uIn = s.nextInt();
        N = uIn;

        for(int i = 0; i < N; i++)
        {

            for(int c = 0; c <= 3; c++)
            {
                uIn = s.nextInt();
                uInArray[ c ] = uIn;
            } //getting n,m,s,t

            n = uInArray [ 0 ];//input
            m = uInArray [ 1 ];//input
            S = uInArray [ 2 ];//input
            T = uInArray [ 3 ];//input
            verArray = new Vertex [ n ];

            for(int z = 0; z < n; z++)
            {
                ver = new Vertex( z );
                ver.curIndex = 0;
                verArray [ z ] = ver;
                verArray [ z ]. adjacencies = new Edge [ 3 ];
            }//creating vertices and adding them to the array and add array of edges to the array of array of edges

            for(int b = 0; b < m; b++)
            {
                for(int k = 0 ; k < 3; k++)
                {
                    uIn = s.nextInt();
                    uInArray1 [ k ] = uIn;
                }

                v1 = uInArray1[ 0 ];//input
                v2 = uInArray1[ 1 ];//input
                W = uInArray1 [ 2 ];//input

                ed = new Edge(verArray [ v1 ] , W );
                verArray[ v2 ]. adjacencies [ verArray[ v2 ].curIndex ] = ed;//adding the edge in the array of edges of the vertex
                verArray[ v2 ].curIndex = verArray[ v2 ].curIndex + 1;//incrementing the index of the array of edges
                                                                      //so if i am going to add another edge to this vertex
                                                                      //it is added in the next index and do not overwite the prev.

                ed = new Edge(verArray [ v2 ] , W );
                verArray[ v1 ] . adjacencies [ verArray [ v1 ].curIndex ] = ed;//adding the edge in the array of edges of the vertex
                verArray[ v1 ].curIndex = verArray[ v1 ].curIndex + 1;//incrementing the index of the array of edges
                                                                      //so if i am going to add another edge to this vertex
                                                                      //it is added in the next index and do not overwite the prev.
              
            }
           
                verArray[ S ].previous = null;
                verArray[ S ].minDistance = 0.0;

                pq = new PriorityQueue<Vertex>();


                pq.add( verArray[ S ] );

                while(!pq.isEmpty())
                 {
                     u = pq.poll();
                     for(int g = 0; g < u.adjacencies.length; g++)
                     {
                         System.out.println("weight"+u.adjacencies[ g ].weight);//test
                         System.out.println("u min"+u.minDistance);//test
                         System.out.println("v min"+v.minDistance);//test

                           if((u.adjacencies[ g ].weight + u.minDistance) < v.minDistance)
                           {

                           v.previous = u;
                           v.minDistance = u.adjacencies[ g ].weight + u.minDistance;
                           pq.add(v);
                           }
                    }
                }
            System.out.println("case " + ( i + 1 ) + " : " + verArray [ T ].minDistance);
        }
    }
}


thanks :detective:

This post has been edited by kngofdrkns: 01 December 2010 - 08:57 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Null Pointer Exception

#2 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: Null Pointer Exception

Posted 01 December 2010 - 03:07 PM

Can you please post the exact error message you get? It would help to debug :)
Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8065
  • View blog
  • Posts: 31,308
  • Joined: 06-March 08

Re: Null Pointer Exception

Posted 01 December 2010 - 05:15 PM

and use descriptive variables names.
How can fe follow the logic of a code with variables like: uIn,n,m,S,T,N,W,v1,v2
Was This Post Helpful? 0
  • +
  • -

#4 kngofdrkns  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 82
  • Joined: 21-May 08

Re: Null Pointer Exception

Posted 02 December 2010 - 03:14 AM

This is the erorr:
Exception in thread "main" java.lang.NullPointerException
at Main.main(Main.java:110)
Java Result: 1
and here is the modified code:
class edge
static class Edge
{
    public Vertex target;
    public double weight;
    public Edge(Vertex argTarget, double argWeight)
    { target = argTarget; weight = argWeight; }
}



class vertex
static class Vertex implements Comparable<Vertex>
{
    public int name, curIndex;
    public Edge[] adjacencies;
    public double minDistance = Double.POSITIVE_INFINITY;
    public Vertex previous;
    public Vertex(int argName) { name = argName; }
    public int compareTo(Vertex other)
    {
        return Double.compare(minDistance, other.minDistance);
    }

}



the main class
import java.util.PriorityQueue;
import java.util.Scanner;

public class Main {

    static class Vertex implements Comparable<Vertex>
{
    public int name, curIndex;
    public Edge[] adjacencies;
    public double minDistance = Double.POSITIVE_INFINITY;
    public Vertex previous;
    public Vertex(int argName) { name = argName; }
    public int compareTo(Vertex other)
    {
        return Double.compare(minDistance, other.minDistance);
    }

}


static class Edge
{
    public Vertex target;
    public double weight;
    public Edge(Vertex argTarget, double argWeight)
    { target = argTarget; weight = argWeight; }
}


    public static void main(String[] args) {
        // TODO code application logic here
        int userInput, numberOfServers, numberOfCables,
                Source,destination,numberOfCases,
                weight,server1,server2;
        int userInputArray []= new int [ 4 ];
        int[] userInputArray1 = new int [ 3 ];
        Scanner s = new Scanner(System.in);
        Vertex ver,u,v = null;
        Edge ed;
        Vertex[] verArray;
        PriorityQueue<Vertex> pq;

        userInput = s.nextInt();
        numberOfCases = userInput;

        for(int i = 0; i < numberOfCases; i++)
        {

            for(int c = 0; c <= 3; c++)
            {
                userInput = s.nextInt();
                userInputArray[ c ] = userInput;
            } //getting n,m,s,t

            numberOfServers = userInputArray [ 0 ];//input
            numberOfCables = userInputArray [ 1 ];//input
            Source = userInputArray [ 2 ];//input
            destination = userInputArray [ 3 ];//input
            verArray = new Vertex [ numberOfServers ];

            for(int z = 0; z < numberOfServers; z++)
            {
                ver = new Vertex( z );
                ver.curIndex = 0;
                verArray [ z ] = ver;
                verArray [ z ]. adjacencies = new Edge [ 3 ];
            }//creating vertices and adding them to the array and add array of edges to the array of array of edges

            for(int b = 0; b < numberOfCables; b++)
            {
                for(int k = 0 ; k < 3; k++)
                {
                    userInput = s.nextInt();
                    userInputArray1 [ k ] = userInput;
                }

                server1 = userInputArray1[ 0 ];//input
                server2 = userInputArray1[ 1 ];//input
                weight = userInputArray1 [ 2 ];//input

                ed = new Edge(verArray [ server1 ] , weight );
                verArray[ server2 ]. adjacencies [ verArray[ server2 ].curIndex ] = ed;//adding the edge in the array of edges of the vertex
                verArray[ server2 ].curIndex = verArray[ server2 ].curIndex + 1;//incrementing the index of the array of edges
                                                                      //so if i am going to add another edge to this vertex
                                                                      //it is added in the next index and do not overwite the prev.

                ed = new Edge(verArray [ server2 ] , weight );
                verArray[ server1 ] . adjacencies [ verArray [ server1 ].curIndex ] = ed;//adding the edge in the array of edges of the vertex
                verArray[ server1 ].curIndex = verArray[ server1 ].curIndex + 1;//incrementing the index of the array of edges
                                                                      //so if i am going to add another edge to this vertex
                                                                      //it is added in the next index and do not overwite the prev.

            }

                verArray[ Source ].previous = null;
                verArray[ Source ].minDistance = 0.0;

                pq = new PriorityQueue<Vertex>();


                pq.add( verArray[ Source ] );

                while(!pq.isEmpty())
                 {
                     u = pq.poll();
                     for(int g = 0; g < u.adjacencies.length; g++)
                     {
                         System.out.println("weight"+u.adjacencies[ g ].weight);//test
                         System.out.println("u min"+u.minDistance);//test
                         System.out.println("v min"+v.minDistance);//test

                           if((u.adjacencies[ g ].weight + u.minDistance) < v.minDistance)
                           {

                           v.previous = u;
                           v.minDistance = u.adjacencies[ g ].weight + u.minDistance;
                           pq.add(v);
                           }
                    }
                }
            System.out.println("case " + ( i + 1 ) + " : " + verArray [ destination ].minDistance);
        }
    }
}




thanks :detective:
Was This Post Helpful? 0
  • +
  • -

#5 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 971
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Null Pointer Exception

Posted 02 December 2010 - 04:38 AM

Variable is null on first access:

...
Vertex ver, u, v = null;
...
System.out.println("v min" + v.minDistance);//test
...

Was This Post Helpful? 0
  • +
  • -

#6 kngofdrkns  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 82
  • Joined: 21-May 08

Re: Null Pointer Exception

Posted 02 December 2010 - 04:48 AM

my input

2
2 1 0 1
0 1 100

output

weight100.0
u min0.0

error
Exception in thread "main" java.lang.NullPointerException
at assignment.Main.main(Main.java:110)
Java Result: 1

code:


package assignment;

        import java.util.PriorityQueue;
	import java.util.Scanner;

	public class Main {

	    static class Vertex implements Comparable<Vertex>
	{
	    public int name, curIndex;
	    public Edge[] adjacencies;
	    public double minDistance = Double.POSITIVE_INFINITY;
	    public Vertex previous;
	    public Vertex(int argName) { name = argName; }
	    public int compareTo(Vertex other)
	    {
	        return Double.compare(minDistance, other.minDistance);
	    }

	}


	static class Edge
	{
	    public Vertex target;
	    public double weight;
	    public Edge(Vertex argTarget, double argWeight)
            { target = argTarget; weight = argWeight; }
	}


	    public static void main(String[] args) {
	        // TODO code application logic here
	        int userInput, numberOfServers, numberOfCables,
	                Source,destination,numberOfCases,
	                weight,server1,server2;
	        int userInputArray []= new int [ 4 ];
	        int[] userInputArray1 = new int [ 3 ];
	        Scanner s = new Scanner(System.in);
	        Vertex ver,u;
	        Edge ed;
	        Vertex[] verArray;
	        PriorityQueue<Vertex> pq;

	        userInput = s.nextInt();
	        numberOfCases = userInput;

	        for(int i = 0; i < numberOfCases; i++)
	        {

	            for(int c = 0; c <= 3; c++)
	            {
	                userInput = s.nextInt();
	                userInputArray[ c ] = userInput;
	            } //getting n,m,s,t

	            numberOfServers = userInputArray [ 0 ];//input
	            numberOfCables = userInputArray [ 1 ];//input
	            Source = userInputArray [ 2 ];//input
	            destination = userInputArray [ 3 ];//input
	            verArray = new Vertex [ numberOfServers ];

	            for(int z = 0; z < numberOfServers; z++)
	            {
	                ver = new Vertex( z );
	                ver.curIndex = 0;
	                verArray [ z ] = ver;
	                verArray [ z ]. adjacencies = new Edge [ 3 ];
	            }//creating vertices and adding them to the array and add array of edges to the array of array of edges

	            for(int b = 0; b < numberOfCables; b++)
	            {
	                for(int k = 0 ; k < 3; k++)
	                {
	                    userInput = s.nextInt();
	                    userInputArray1 [ k ] = userInput;
	                }

	                server1 = userInputArray1[ 0 ];//input
	                server2 = userInputArray1[ 1 ];//input
	                weight = userInputArray1 [ 2 ];//input

	                ed = new Edge(verArray [ server1 ] , weight );
	                verArray[ server2 ]. adjacencies [ verArray[ server2 ].curIndex ] = ed;//adding the edge in the array of edges of the vertex
	                verArray[ server2 ].curIndex = verArray[ server2 ].curIndex + 1;//incrementing the index of the array of edges
	                                                                      //so if i am going to add another edge to this vertex
	                                                                      //it is added in the next index and do not overwite the prev.

	                ed = new Edge(verArray [ server2 ] , weight );
                        verArray[ server1 ] . adjacencies [ verArray [ server1 ].curIndex ] = ed;//adding the edge in the array of edges of the vertex
	                verArray[ server1 ].curIndex = verArray[ server1 ].curIndex + 1;//incrementing the index of the array of edges
	                                                                      //so if i am going to add another edge to this vertex
                                                                      //it is added in the next index and do not overwite the prev.

	            }

	                verArray[ Source ].previous = null;
	                verArray[ Source ].minDistance = 0.0;

	                pq = new PriorityQueue<Vertex>();


	                pq.add( verArray[ Source ] );

	                while(!pq.isEmpty())
	                 {
	                     u = pq.poll();
	                     for(int g = 0; g < u.adjacencies.length; g++)
	                     {
	                         System.out.println("weight"+u.adjacencies[ g ].weight);//test
	                         System.out.println("u min"+u.minDistance);//test
                                 Vertex v = u.adjacencies[g].target;
	                           if((u.adjacencies[ g ].weight + u.minDistance) < v.minDistance)
	                           {

	                           v.previous = u;
	                           v.minDistance = u.adjacencies[ g ].weight + u.minDistance;
	                           pq.add(v);
	                           }
	                    }
	                }
	            System.out.println("case " + ( i + 1 ) + " : " + verArray [ destination ].minDistance);
	        }
	    }
	}


it was not the problem n8wxs :helpsmilie:
thanks :detective:

This post has been edited by kngofdrkns: 02 December 2010 - 05:05 AM

Was This Post Helpful? 0
  • +
  • -

#7 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 971
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Null Pointer Exception

Posted 02 December 2010 - 04:55 AM

Quote

it was not the problem n8wxs


Yes it is:

Attached Image

This post has been edited by n8wxs: 02 December 2010 - 05:01 AM

Was This Post Helpful? 0
  • +
  • -

#8 kngofdrkns  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 82
  • Joined: 21-May 08

Re: Null Pointer Exception

Posted 02 December 2010 - 05:04 AM

its not look at the code i added i removed the vertex v initializing from the top and added it to
for(int g = 0; g < u.adjacencies.length; g++)
109	                         {
110	                             System.out.println("weight"+u.adjacencies[ g ].weight);//test
111	                             System.out.println("u min"+u.minDistance);//test
112	                                 Vertex v = u.adjacencies[g].target;
113	                               if((u.adjacencies[ g ].weight + u.minDistance) < v.minDistance)
114	                               {
115	 
116	                               v.previous = u;
117	                               v.minDistance = u.adjacencies[ g ].weight + u.minDistance;
118	                               pq.add(v);
119	                               }


This post has been edited by kngofdrkns: 02 December 2010 - 05:05 AM

Was This Post Helpful? 0
  • +
  • -

#9 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 971
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Null Pointer Exception

Posted 02 December 2010 - 05:41 AM

The adjacencies array only has one entry:

Attached Image

The second pass of the for() loop has g == 1

This post has been edited by n8wxs: 02 December 2010 - 05:41 AM

Was This Post Helpful? 1
  • +
  • -

#10 kngofdrkns  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 82
  • Joined: 21-May 08

Re: Null Pointer Exception

Posted 02 December 2010 - 06:15 AM

View Postn8wxs, on 02 December 2010 - 04:41 AM, said:

The adjacencies array only has one entry:

Attachment null.PNG

The second pass of the for() loop has g == 1

thanks alot i solved the problem by
if(u.adjacencies[g] != null)
                                 {
                                 Vertex v = u.adjacencies[g].target;
	                           if((u.adjacencies[ g ].weight + u.minDistance) < v.minDistance)
	                           {

	                           v.previous = u;
	                           v.minDistance = u.adjacencies[ g ].weight + u.minDistance;
	                           pq.add(v);
	                           }
                                 }



thanks alot for your help and i will get the new netbeans to access the var like u did :bananaman:
Was This Post Helpful? 0
  • +
  • -

#11 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 971
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Null Pointer Exception

Posted 02 December 2010 - 06:51 AM

Learning to using the debugger is highly desirable. :punk:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1