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

Thread: Driver error, null pointer exception when creating an instance of a class?

  1. #1
    Member
    Join Date
    Sep 2012
    Posts
    56
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Driver error, null pointer exception when creating an instance of a class?

    Okay so I have no idea why this is happening. Its not even a question about methodology I am just wondering if I should build a new project or something at this point because I can't see how/why I am getting an error. Here is my code:

    Driver:

    package graphalgorithms;
     
    import java.io.*;
    import java.util.*;
     
    public class GraphAlgorithms {
     
        public static void main(String[] args) throws IOException {
     
            PrimsAlgorithm graph = new PrimsAlgorithm();
     
            String inputFile = "src/input/input.txt";
            String fName = inputFile;
            String thisLine;
            String[][] data = new String[100][100];
            int count = 0;
            int size = 0;
     
            FileInputStream geek = new FileInputStream(fName);
            BufferedReader in = new BufferedReader(new InputStreamReader(geek, "UTF-16"));
     
            while ((thisLine = in.readLine()) != null) {
     
                String[] array = thisLine.split(",");
                System.arraycopy(array, 0, data[count], 0, array.length);
                size = array.length;
                count++;
            }
     
            String[] vert = new String[size];
            System.arraycopy(data[0], 0, vert, 0, size);
     
            String[][] arrayData = new String[size][size];
            for (int i = 0; i < size; i++) {
                System.arraycopy(data[i + 1], 0, arrayData[i], 0, size);
            }
     
            Scanner sc = new Scanner(System.in);
            System.out.println("Please select the algorithm you wish to run: "
                    + "1 for Prim's algorithm, 2 for Kruskal's algorithm"
                    + "or 3 for Floyd-Warshall’s algorithm ");
     
            while (sc.hasNext()) {
     
                int i = sc.nextInt();
                switch (i) {
     
                    case 1:
     
                        System.out.println("You chose Prim's algorithm.");
                        graph.initializeData(arrayData,vert);
                        break;
     
                    case 2:
                        System.out.println("You chose Kruskal's algorithm.");
                        break;
     
                    case 3:
                        System.out.println("You chose Floyd-Warshall's algorithm.");
                        break;
     
                    default:
                        System.out.println("Invalid entry");
                        break;
                }
                System.out.println();
                System.out.println("Please select the algorithm you wish to run: "
                        + "1 for Prim's algorithm, 2 for Kruskal's algorithm"
                        + "or 3 for Floyd-Warshall’s algorithm ");
            }
        }
    }

    Class:

    package graphalgorithms;
     
    public class PrimsAlgorithm {
     
        private String[] verticies;
        private int[][] actualData;
        private final int max = Integer.MAX_VALUE;
        private final int V = verticies.length;
     
        public void initializeData(String[][] incomingArray, String [] incomingVerticies) {
     
            verticies = incomingVerticies;
     
            actualData = new int[incomingArray.length-1][incomingArray.length];
     
            for(int i = 0; i < actualData.length; i++){
                for(int j = 0; j < incomingArray.length; j++){
     
                    if (incomingArray[i][j].equals("\u221E")) {
     
                        actualData[i][j] = 999999;      //if inf symbol is seen replace with high value weight
                    } else {
                        actualData[i][j] = Integer.parseInt(incomingArray[i][j]);
                    }
                }
            }
     
            //primsMST(actualData);
        }
    }

    Now when I run the program it errors at line 10 where I create an instance of class PrimsAlgorithm even though I have ran the program successfully before. I am not sure when this stared happening as a change a few names around but not much then all of a sudden...

    This is the error its giving me:

    run:
    Exception in thread "main" java.lang.NullPointerException
    at graphalgorithms.PrimsAlgorithm.<init>(PrimsAlgorit hm.java:8)
    at graphalgorithms.GraphAlgorithms.main(GraphAlgorith ms.java:10)
    Java Result: 1
    BUILD SUCCESSFUL (total time: 0 seconds)

    Any ideas? This is line 10:

    PrimsAlgorithm graph = new PrimsAlgorithm();

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Driver error, null pointer exception when creating an instance of a class?

    Exception in thread "main" java.lang.NullPointerException
    at graphalgorithms.PrimsAlgorithm.<init>(PrimsAlgorit hm.java:8)
    What variable was null when line 8 was executed?
    Look at line 8 and find the variable with the null value.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Sep 2012
    Posts
    56
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Driver error, null pointer exception when creating an instance of a class?

    Quote Originally Posted by Norm View Post
    What variable was null when line 8 was executed?
    Look at line 8 and find the variable with the null value.
    So its odd because there is no variable in line 8. Line 8 is the method deceleration:

    public static void main(String[] args) throws IOException {


    --- Update ---

    Yeah I can't seem to figure out why this isn't working. I tried creating a new project but got the same error.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Driver error, null pointer exception when creating an instance of a class?

    Exception in thread "main" java.lang.NullPointerException
    at graphalgorithms.PrimsAlgorithm.<init>(PrimsAlgorithm.java:8)
    The problem is in the PrimsAlgorithm class. Look at line 8 there.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Sep 2012
    Posts
    56
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Driver error, null pointer exception when creating an instance of a class?

    Wow, okay I am sorry. I guess I just missed that. Thank you!

Similar Threads

  1. How can i fix my null pointer exception error?
    By jomdaime in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 16th, 2014, 06:00 PM
  2. Null Pointer Exception error
    By Keitho55 in forum Exceptions
    Replies: 3
    Last Post: November 18th, 2011, 03:29 AM
  3. NULL POINTER EXCEPTION error
    By beefwithrice in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 28th, 2011, 06:26 AM
  4. JComboBox null pointer Exception error
    By F_Arches in forum AWT / Java Swing
    Replies: 2
    Last Post: November 29th, 2009, 02:32 PM
  5. Getting Null Pointer Exception in runtime
    By RoadRunner in forum Exceptions
    Replies: 1
    Last Post: April 26th, 2009, 01:21 PM