This is giving me a nullpointerexception:

public static final int[][] ARRAY = {


public static boolean method1() {

	for (int[] data : ARRAY) {
		int numberOne = data[0];

	if (numberOne != numberTwo) {

public static boolean method2(int number) {

	int count = getCount(number);


I think it's a problem because it's trying to send a non-static number (numberOne) to another method. If it's this, how can I make "int numberOne = data[0];" static?

Recommended Answers

All 5 Replies

This is giving me a nullpointerexception:

What statement does it happen on?

Hmm... Your code at line 8, I think your loop syntax is incorrect. To access each item in an array, you need...

for (int i=0; i<data.length; i++) {

I assume that "data" is the class's variable. What do you want to do with method1() and method2() by the way? Also, how do you use them in your class or other classes? You should have comments for each method you are doing... Also, you should copy & paste the error you got because the error explains a lot about what the cause is...

Taywin, the syntax the OP used is also valid. It's a for-each loop.


// String[] names
for (String name : names) { ... }

// int[][] ARRAY
for (int[] data : ARRAY) { ... }

However I think this is the place where the Exception may be thrown. If NUMBERS is not an int[] or it is not initialised, that would cause problems.

Feriscool, please give us more information (including the stack trace for the NPE).

Oh thanks for the info. I don't use that format often enough to remember it. :P

Anyway, you are passing a local variable in "static" method to another "static" method here. If you want to make numberOne static, you could static int numberOne = data[0]; and pass the value into another static method?

However, why would you design the class to be that way? If you want, you should call the method1() and get the returned value, and then use the value to call method2() from the same caller. The way you design to use a static method calls another static method in this case is strange...

I don't see anything particularly illegal about the use of static and local variables here, but the code as posted is clearly wrong:

for (int[] data : ARRAY) {
  int numberOne = data[0]; // scope of numberOne is this for loop
  // totally usless loop - copies multiple values to local var,
  // each overwriting the previous, then lets it go out of scope
  // without ever being used for anything.
if (numberOne != numberTwo) { // two undefined variables

Anyway, the NPE message includes the LINE NUMBER where the E was thrown, so please share that with us!

ps It's interesting, given the posted code, that it's an NPE (the vars are all arrays or primitives) - not an index out of range

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.