Accessing method from another class, NullPointerException
up vote
0
down vote
favorite
so I've been wracking my brains over why this isn't working for an hour now. I have two classes, class One containing an ArrayList of ArrayLists and a method to add another element to that list, and class Two, from which I'm trying to access that method.
public class One
{
private ArrayList<Element> myarraylist;
public One()
{
myarraylist = new ArrayList<Element>();
}
public void addElement(String name)
{
myarraylist.add(new Element(name));
}
}
//Element being another class
public class Two
{
One database;
public static void main(String args)
{
Two two = new Two();
two.startMenu();
}
public Two()
{
One database = new One();
}
public void addElem()
{
Scanner keyboard = new Scanner(System.in);
String name = keyboard.next();
database.addElement(name);
}
}
//where startMenu is just a small multiple choice menu thingy
Problem is, when I try to run through it and I get to the last line, I get the message: java.lang.NullPointerException
I tried inspecting the objects (I use BlueJ), and the ArrayList is initialized when I just make an instance of class One, but when I make an instance of class Two, the database instance is null.
Thanks in advance for your answers :D
java arraylist
add a comment |
up vote
0
down vote
favorite
so I've been wracking my brains over why this isn't working for an hour now. I have two classes, class One containing an ArrayList of ArrayLists and a method to add another element to that list, and class Two, from which I'm trying to access that method.
public class One
{
private ArrayList<Element> myarraylist;
public One()
{
myarraylist = new ArrayList<Element>();
}
public void addElement(String name)
{
myarraylist.add(new Element(name));
}
}
//Element being another class
public class Two
{
One database;
public static void main(String args)
{
Two two = new Two();
two.startMenu();
}
public Two()
{
One database = new One();
}
public void addElem()
{
Scanner keyboard = new Scanner(System.in);
String name = keyboard.next();
database.addElement(name);
}
}
//where startMenu is just a small multiple choice menu thingy
Problem is, when I try to run through it and I get to the last line, I get the message: java.lang.NullPointerException
I tried inspecting the objects (I use BlueJ), and the ArrayList is initialized when I just make an instance of class One, but when I make an instance of class Two, the database instance is null.
Thanks in advance for your answers :D
java arraylist
Your code is incomplete. Your complaining on exception (which you didn't provide), and the parts of code to which exception supposedly related is missing.
– muradm
Nov 11 at 20:07
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
so I've been wracking my brains over why this isn't working for an hour now. I have two classes, class One containing an ArrayList of ArrayLists and a method to add another element to that list, and class Two, from which I'm trying to access that method.
public class One
{
private ArrayList<Element> myarraylist;
public One()
{
myarraylist = new ArrayList<Element>();
}
public void addElement(String name)
{
myarraylist.add(new Element(name));
}
}
//Element being another class
public class Two
{
One database;
public static void main(String args)
{
Two two = new Two();
two.startMenu();
}
public Two()
{
One database = new One();
}
public void addElem()
{
Scanner keyboard = new Scanner(System.in);
String name = keyboard.next();
database.addElement(name);
}
}
//where startMenu is just a small multiple choice menu thingy
Problem is, when I try to run through it and I get to the last line, I get the message: java.lang.NullPointerException
I tried inspecting the objects (I use BlueJ), and the ArrayList is initialized when I just make an instance of class One, but when I make an instance of class Two, the database instance is null.
Thanks in advance for your answers :D
java arraylist
so I've been wracking my brains over why this isn't working for an hour now. I have two classes, class One containing an ArrayList of ArrayLists and a method to add another element to that list, and class Two, from which I'm trying to access that method.
public class One
{
private ArrayList<Element> myarraylist;
public One()
{
myarraylist = new ArrayList<Element>();
}
public void addElement(String name)
{
myarraylist.add(new Element(name));
}
}
//Element being another class
public class Two
{
One database;
public static void main(String args)
{
Two two = new Two();
two.startMenu();
}
public Two()
{
One database = new One();
}
public void addElem()
{
Scanner keyboard = new Scanner(System.in);
String name = keyboard.next();
database.addElement(name);
}
}
//where startMenu is just a small multiple choice menu thingy
Problem is, when I try to run through it and I get to the last line, I get the message: java.lang.NullPointerException
I tried inspecting the objects (I use BlueJ), and the ArrayList is initialized when I just make an instance of class One, but when I make an instance of class Two, the database instance is null.
Thanks in advance for your answers :D
java arraylist
java arraylist
edited Nov 11 at 20:07
asked Nov 11 at 19:49
HippieDruid
32
32
Your code is incomplete. Your complaining on exception (which you didn't provide), and the parts of code to which exception supposedly related is missing.
– muradm
Nov 11 at 20:07
add a comment |
Your code is incomplete. Your complaining on exception (which you didn't provide), and the parts of code to which exception supposedly related is missing.
– muradm
Nov 11 at 20:07
Your code is incomplete. Your complaining on exception (which you didn't provide), and the parts of code to which exception supposedly related is missing.
– muradm
Nov 11 at 20:07
Your code is incomplete. Your complaining on exception (which you didn't provide), and the parts of code to which exception supposedly related is missing.
– muradm
Nov 11 at 20:07
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
accepted
The problem is at the following lines
public class Two
{
One database;
public Two()
{
One database = new One();
//this variable is not the same as the one declared outside the constructor
}
When declaring a variable in a method which has the same name as the variable declared in the class, the modifications which happen on the variable declared inside the method will not be seen in the variable outside the method(because the 2 variables are different). The variable in the method is shadowing the variable from the class.
To differentiate between the two variables you must use the this keyword
this.database = new One();
The final solution should be
public class Two
{
One database;
public Two()
{
this.database = new One();
}
The reason you get the NullPointerException when doing database.addElement(name); is because in your example the database is not instatiated beacause the object created is stored in a different variable named database and not the one declared as a class attribute.
Oh my God, thank you so much! I'm literally a baby at this, you just saved my life!
– HippieDruid
Nov 11 at 20:11
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53252564%2faccessing-method-from-another-class-nullpointerexception%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
The problem is at the following lines
public class Two
{
One database;
public Two()
{
One database = new One();
//this variable is not the same as the one declared outside the constructor
}
When declaring a variable in a method which has the same name as the variable declared in the class, the modifications which happen on the variable declared inside the method will not be seen in the variable outside the method(because the 2 variables are different). The variable in the method is shadowing the variable from the class.
To differentiate between the two variables you must use the this keyword
this.database = new One();
The final solution should be
public class Two
{
One database;
public Two()
{
this.database = new One();
}
The reason you get the NullPointerException when doing database.addElement(name); is because in your example the database is not instatiated beacause the object created is stored in a different variable named database and not the one declared as a class attribute.
Oh my God, thank you so much! I'm literally a baby at this, you just saved my life!
– HippieDruid
Nov 11 at 20:11
add a comment |
up vote
0
down vote
accepted
The problem is at the following lines
public class Two
{
One database;
public Two()
{
One database = new One();
//this variable is not the same as the one declared outside the constructor
}
When declaring a variable in a method which has the same name as the variable declared in the class, the modifications which happen on the variable declared inside the method will not be seen in the variable outside the method(because the 2 variables are different). The variable in the method is shadowing the variable from the class.
To differentiate between the two variables you must use the this keyword
this.database = new One();
The final solution should be
public class Two
{
One database;
public Two()
{
this.database = new One();
}
The reason you get the NullPointerException when doing database.addElement(name); is because in your example the database is not instatiated beacause the object created is stored in a different variable named database and not the one declared as a class attribute.
Oh my God, thank you so much! I'm literally a baby at this, you just saved my life!
– HippieDruid
Nov 11 at 20:11
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
The problem is at the following lines
public class Two
{
One database;
public Two()
{
One database = new One();
//this variable is not the same as the one declared outside the constructor
}
When declaring a variable in a method which has the same name as the variable declared in the class, the modifications which happen on the variable declared inside the method will not be seen in the variable outside the method(because the 2 variables are different). The variable in the method is shadowing the variable from the class.
To differentiate between the two variables you must use the this keyword
this.database = new One();
The final solution should be
public class Two
{
One database;
public Two()
{
this.database = new One();
}
The reason you get the NullPointerException when doing database.addElement(name); is because in your example the database is not instatiated beacause the object created is stored in a different variable named database and not the one declared as a class attribute.
The problem is at the following lines
public class Two
{
One database;
public Two()
{
One database = new One();
//this variable is not the same as the one declared outside the constructor
}
When declaring a variable in a method which has the same name as the variable declared in the class, the modifications which happen on the variable declared inside the method will not be seen in the variable outside the method(because the 2 variables are different). The variable in the method is shadowing the variable from the class.
To differentiate between the two variables you must use the this keyword
this.database = new One();
The final solution should be
public class Two
{
One database;
public Two()
{
this.database = new One();
}
The reason you get the NullPointerException when doing database.addElement(name); is because in your example the database is not instatiated beacause the object created is stored in a different variable named database and not the one declared as a class attribute.
edited Nov 11 at 20:08
answered Nov 11 at 20:00
Alexandru Stana
645
645
Oh my God, thank you so much! I'm literally a baby at this, you just saved my life!
– HippieDruid
Nov 11 at 20:11
add a comment |
Oh my God, thank you so much! I'm literally a baby at this, you just saved my life!
– HippieDruid
Nov 11 at 20:11
Oh my God, thank you so much! I'm literally a baby at this, you just saved my life!
– HippieDruid
Nov 11 at 20:11
Oh my God, thank you so much! I'm literally a baby at this, you just saved my life!
– HippieDruid
Nov 11 at 20:11
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53252564%2faccessing-method-from-another-class-nullpointerexception%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Your code is incomplete. Your complaining on exception (which you didn't provide), and the parts of code to which exception supposedly related is missing.
– muradm
Nov 11 at 20:07