Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Access an array, from different class. How?

 
Pan Niko
Ranch Hand
Posts: 86
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I have two classes





Thank you
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you post the stack-trace?
 
Pan Niko
Ranch Hand
Posts: 86
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Classes name are random by the way
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The stacktrace says- Nullpointer at Line 17 in B.javain the method legalMove(). Can you show us the actual code? As the one you have mentioned earlier and the stacktrace are not similar.
 
Pan Niko
Ranch Hand
Posts: 86
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok my apologies. Here is the code


If the row of first clicked button is equal with second or columns then return true and print out, else false

The line 111 in class A just calling this method
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
With the given code- really difficult to tell the reason for the Nullpointer Exception. But either board is NULL or board.getPocketArray(0, 0) is null. You would have to check that. Also check if the instances being referred in the if condition of the legalMove method are not null.

 
Pan Niko
Ranch Hand
Posts: 86
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok i tested it with some system.out and...


Nothing printed on the screen

and


Place is NULL printed on Screen.

I cant understand why that place is NULL i have already initialise it. :s
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think we would have to look at the code. Can you provide us the complete necessary code to help you resolve this issue?
 
Pan Niko
Ranch Hand
Posts: 86
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is around 6 classes with super and subclasses my whole programm. Still you want to check it?? I found where the null begining, getArray() method returns null


 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you initialize the individual elements on the array variable? You need to if you haven't.

This


just creates an array object whose elements are null.
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is because you initialized the array but not the array elements. You'll need to iterate through the array and initialize the individual elements.
 
Pan Niko
Ranch Hand
Posts: 86
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmm...can i have a simple example for this please??
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


You can extend the same for a 2D array- You will have 2 loops (1 outer loop and 1 Inner loop). Something like this-
>
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic