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 3 of 3

Thread: Something wrong with array

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

    Default Something wrong with array

    import java.io.*;
    public class Prim {
    	public static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
     
    ....
     
            public static tempEdge [] tedges;
    ....
     
            public Graph graph;
     
            private static int tempi = 0, one, two, size;
     
            public void buildTable(String file){
     
                n = 10;//menu.numOfNodes();
                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");
                }
                prims();
            }
            //create a minal spanning tree
    	public static void prims()
    	{
                    n = 5;//menu.numOfNodes();
                    //set size of the array which store different things
    		Graph = new int[n+1][n+1];
    		Tree = new int[n+1][3];
    		near = new int[n+1];
     
                    while(tedges != null){
                       for(int i=0;i<=tedges.length;i++){
                            //tempEdge e = tedges[i];
                                one = tedges[i].firstPoint();
                                System.out.println("1");
                                two = tedges[i].secondPoint();
                                size = tedges[i].distance();
                                if((one!=two)&&(one<two)) {
                                    Graph[one][two] = Graph[two][one] = size;
                                    if(Graph[one][two] == 0 )
                                        Graph[two][one] = Graph[one][two] = 7001;
                                }
                                if(one==two)
                                    Graph[one][two]=7001;
                           }
                        }
     
    		getMinKL();
    		mincost = Graph[k][l];
    		Tree[1][1] = l;
    		Tree[1][2] = k;
    		for(int i=1; i<=n; i++)
    			near[i] = (Graph[i][l]<Graph[i][k])?l:k;
    		near[k] = near[l] = 0;
     
    		for(int i=2; i<n; i++)
    		{
    			int j = getMin();
    			Tree[i][1] = j; Tree[i][2] = near[j];
    			mincost = mincost+Graph[j][near[j]];
    			near[j] =0;
    			for (int k=1; k<=n; k++)
    				if( (near[k] !=0) && Graph[k][ near[k] ]> Graph[k][j] )
    					near[k] =j;
    		}
                    tri.createNode();
    	}
    .......
     
            public static void main (String[] args) throws IOException
            {
                   Prim prim = new Prim();
                   prim.buildTable("3Dtriangle.txt");
     
                   System.out.println("Solution : ");
    		for (int i = 1; i<=n; i++)
    		{
    			if( (Tree[i][1]!=0) && (Tree[i][2] !=0) )
    				System.out.println(Tree[i][1] + "-" +Tree[i][2]);
                                    System.out.println(Graph[Tree[i][1]][Tree[i][2]]);
    		}
    	}
    }
    I got a file with 6 line in it and
    After i run the code, it shows
    1
    2
    50
    1
    3
    Exception in thread "main" java.lang.NullPointerException
    60
    1
    4
    70
    2
    3
    80
    2
    4
    40
    3
    4
    60
    1
    1
    1
    1
    1
    1
    at triangle_projection.Prim.prims(Prim.java:69)
    at triangle_projection.Prim.buildTable(Prim.java:55)
    at triangle_projection.Prim.main(Prim.java:178)

    can anyone please help me to solve it?
    Thank you
    Last edited by JavaPF; October 25th, 2010 at 06:35 AM. Reason: Please use [highlight=Java] [/highlight] tags


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Something wrong with array

    Is this your entire code? Please show us the content of the file.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

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

    Default Re: Something wrong with array

    The file contains:
    1 2 50
    1 3 60
    1 4 70
    2 3 80
    2 4 40
    3 4 60

Similar Threads

  1. WHAT IS WRONG HERE PLEASE?
    By mjava in forum What's Wrong With My Code?
    Replies: 3
    Last Post: August 27th, 2010, 08:29 PM
  2. Something is wrong? Please help.
    By DestinyChick1225 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 29th, 2010, 07:47 AM
  3. What's wrong?!
    By deeerek in forum What's Wrong With My Code?
    Replies: 6
    Last Post: February 22nd, 2010, 07:11 PM
  4. don't know what's wrong
    By james in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 15th, 2010, 07:37 PM
  5. Where am I going wrong? :)
    By KevinGreen in forum What's Wrong With My Code?
    Replies: 9
    Last Post: October 18th, 2009, 12:03 AM