0

I am pretty new to java but i have to initialize an 2d-array size n, in this example 10. After initialization i want to check the diagonal entries if they are false, and if set them to true. After i want to return value of i.

This is what i coded:

First of all initialization of the array:

public static void init(int n) {
        boolean friendship[][] = new boolean[n][n];}

and after i tried this:

public static int addUser(String name) {
        int id=0;
        for ( int i=0;i<friendship.length;i++) {
            if ( friendship[i][i] = false) {
                friendship[i][i] = true;
                id = i;
            }
        }
        return id;
    }

Sadly its throwing:

Exception in thread "main" java.lang.NullPointerException
    at x.SocialNetwork.addUser(SocialNetwork.java:18)
    at x.SocialNetwork.main(SocialNetwork.java:53)

What can i do to fix this?

PS: Sorry for bad english and formatting.

1
  • 2
    Why are you bothering to check if they're false? Why not just set them to true regardless? (by the way, use == instead of = to compare things) Commented Nov 17, 2013 at 16:58

1 Answer 1

3

I assume you have a static field called friendship. In this method

public static void init(int n) {
     boolean friendship[][] = new boolean[n][n];
}

you are declaring a new local friendship variable, that is shadowing the static member. Therefore, the static friendship field remains null and when you try to access it in addUser you get a NullPointerException.

Use

public static void init(int n) {
     friendship = new boolean[n][n];
}

assuming again that you have something like

public static boolean[][] friendship;

In this

if ( friendship[i][i] = false) {

you are actually setting friendship[i][i] to false. The equality operator is ==.


This is how I see your class

public class Test {
    /* visibility identifier doesn't matter */ static boolean[][] friendship;

    public static void init(int n) {
        // this is a different variable from the member declared above
        // it is a local variable
        boolean friendship[][] = new boolean[n][n]; 
    }

    public static int addUser(String username) {
        int id=0;
        for ( int i=0;i<friendship.length;i++) {
            if ( friendship[i][i] = false) { // referring to static field, not the local variable in init()
                friendship[i][i] = true;
                id = i;
            }
        }
        return id;
    }
}
Sign up to request clarification or add additional context in comments.

35 Comments

OP doesn't have a static field, and what is a static init? Is it a static initializer? Why this static needed here?
@nikpon Using static here is just a design choice. It's not the real issue. The issue is OP has two variables that have the same name, one static and one local. The local one is shadowing the static one.
Where did you see the design here? You are absolutely incorrect about static.
@nikpon You could get rid of the static and OP would have the same problem assuming that they have an instance field called friendship.
Nope, this absolutely not necessary, the same problem is not the issue, and you have nothing to improve the OP problem
|

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.