0

Ok, so I have a global array as part of the Class "Temp". I want to edit that array from anywhere in the program. That is working fine. However, when I try to use those values to set values in my "Cords" Class, I get a Null Exception Error. I have commented it in my code. Any idea why?

import java.util.Scanner;


public class Solution {

    public static void main(String[] args) {


        Scanner inp = new Scanner(System.in);

        int n = 0;

        for(int k = 0; k<4; k++){

            Temp.tempValues[k] = 0;

        }

        boolean checkNums = false;

        String coords = "";


        while(n<1 || n>2000){

            System.out.println("Enter number of lines");

            n = inp.nextInt();


        }

        Cords[] lines = new Cords[n];

        int proceed = 0;

        inp.nextLine();

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

            proceed = 0;

            checkNums = false;

            while((proceed != 3) || (checkNums == false)){

                System.out.println("Enter line coordinates. "+(i+1));

                coords = inp.nextLine();

                proceed = checkSpaces(coords);

                checkNums = rightNums(coords);

            }

            lines[i] = new Cords();

            lines[i].setValues(Temp.tempValues[0], Temp.tempValues[1], Temp.tempValues[2], Temp.tempValues[3]);


        }

    }

    public static int checkSpaces(String sent){

        int spaces = 0;

        for(int y = 0; y< sent.length(); y++){

            if(sent.charAt(y)==' '){

                spaces++;

            }

        }

        return spaces;

    }

    public static boolean rightNums(String sent){


        int z = 0;

        int l = 0;

        String num = "";

        while(z<sent.length()){

                while((z<sent.length())&&(sent.charAt(z) != ' ')){

                    num += sent.charAt(z);

                    z++;

                }

                if(Integer.parseInt(num) < 0 || Integer.parseInt(num) >=10000){

                    return false;

                }else{

                    Temp.tempValues[l] = Integer.parseInt(num);

                    num = "";
                    z++;

                    l++;
                }

            }

        return true;

    }

    public class Cords{

        int x1 = 0;
        int x2 = 0;
        int y1 = 0;
        int y2 = 0;

        public void setValues(int xx1, int yy1, int xx2, int yy2){

            x1 = xx1;
            y1 = yy1;
            x2 = xx2;
            y2 = yy2;

        }

}

    public static class Temp{

        static int[] tempValues = new int[4];

    }

}
4
  • possible duplicate of What is a Null Pointer Exception, and how do I fix it? Commented Nov 11, 2014 at 12:06
  • One cannot have, two public class in single file. Commented Nov 11, 2014 at 12:22
  • It is needed, as I will be posting code online, and can only be read from the one file Commented Nov 11, 2014 at 12:22
  • Try to remove "public" from that cord class, and then try your example Commented Nov 11, 2014 at 12:25

2 Answers 2

4

Your lines array does not contain any object. It just contains nulls and you can't invoke methods on null-values.

Since you are iterating over your lines array, initialize the current element before you invoke methods on it:

for (int i = 0; i < lines.length; i++) {
    lines[i] = new Cords();

    lines[i].setValues(...);
}

You also have to make the Cords class static.

Sign up to request clarification or add additional context in comments.

7 Comments

Sorry, i is in a for loop, my mistake. Forgot to type it out
at lines[i] = new Cords() I get error: No enclosing instance of type solution is accessible. Must qualify the allocation with enclosing instance of type solution. (Eg. x.new A()where x is an instance of Solution
Please show all of your code. Otherwise we can't help.
A lot of it is probably distracting
If Cords class is static, can I still make objects?
|
0
public static void main(String[] args){

    Cords[] lines = new Cords[5];

    for(int k = 0; k<4; k++){
        Temp.tempValues[k] = 0;
    }

    for(int i = 0; i < lines.length; i++){
        lines[i] = new Cords();
    lines[i].setValues(Temp.tempValues[0], Temp.tempValues[1], Temp.tempValues[2], Temp.tempValues[3]);
    }
}

3 Comments

No enclosing instance of type solution is accessible. Must qualify the allocation with enclosing instance of type solution. (Eg. x.new A()where x is an instance of Solution
at lines[i] = new Cords()
@JohnDoe what do you want to say?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.