Attempt to invoke virtual method '' on a null object reference [duplicate]
This question already has an answer here:
What is a NullPointerException, and how do I fix it?
12 answers
I'm getting this error "Attempt to invoke virtual method 'java.lang.String eecs1022.caps.Game.qa()' on a null object reference"
I just want to know why im getting this error and how I can work around it.
The game object is returns a string but for some reason it says its a null object. Here is my main class and my game class.
MAIN:
private Game game;
private String question = "";
private String answer = "";
private int score = 1;
private int qNum = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ask();
}
public void ask(){
String apple = game.qa();
int indexanswer = apple.indexOf("n");
answer = apple.substring(indexanswer);
question = apple.substring(0,indexanswer);
((TextView)findViewById(R.id.question)).setText(question);
}
public void onDone (View v){
if (qNum == 10){
finish();
}
String answerinput = ((EditText)findViewById(R.id.answer)).toString();
if (answer.equalsIgnoreCase(answerinput)){
score++;
}
String oldlog = ((TextView)findViewById(R.id.log)).toString();
String a = oldlog + "n" + "Q# " + qNum + ": " + question + "n" + "Your answer" + answerinput.toUpperCase() + "n" + "Correct Answer: " + answer + "n";
((TextView)findViewById(R.id.log)).setText(a);
qNum++;
String orange = "SCORE = " + score;
String banana = "Q# " + qNum;
((TextView)findViewById(R.id.score)).setText(orange);
((TextView)findViewById(R.id.qNum)).setText(banana);
ask();
}
}
GAME:
public class Game {
public String capitalanswer = "";
public String countryanswer = "";
private CountryDB db;
public Game(){
this.db = new CountryDB();
}
public String qa(){
List<String> countries = db.getCapitals();
int n = countries.size();
int index = (int)( n * Math.random());
String c = countries.get(index);
capitalanswer = c;
System.out.println(capitalanswer);
Map<String, Country> data = db.getData();
Country ref = data.get(c);
countryanswer = ref.toString();
System.out.println(countryanswer);
if (Math.random() < 0.5){
return "What country has the capital " + ref.getCapital() + " ?" + "n" + ref.getName();
}else{
return "What is the capital of " + ref.getName() + " ?" + "n" + ref.getCapital();
}
}
}
Any help is appreciated!
java
marked as duplicate by Andy Turner
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 22 at 20:05
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
What is a NullPointerException, and how do I fix it?
12 answers
I'm getting this error "Attempt to invoke virtual method 'java.lang.String eecs1022.caps.Game.qa()' on a null object reference"
I just want to know why im getting this error and how I can work around it.
The game object is returns a string but for some reason it says its a null object. Here is my main class and my game class.
MAIN:
private Game game;
private String question = "";
private String answer = "";
private int score = 1;
private int qNum = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ask();
}
public void ask(){
String apple = game.qa();
int indexanswer = apple.indexOf("n");
answer = apple.substring(indexanswer);
question = apple.substring(0,indexanswer);
((TextView)findViewById(R.id.question)).setText(question);
}
public void onDone (View v){
if (qNum == 10){
finish();
}
String answerinput = ((EditText)findViewById(R.id.answer)).toString();
if (answer.equalsIgnoreCase(answerinput)){
score++;
}
String oldlog = ((TextView)findViewById(R.id.log)).toString();
String a = oldlog + "n" + "Q# " + qNum + ": " + question + "n" + "Your answer" + answerinput.toUpperCase() + "n" + "Correct Answer: " + answer + "n";
((TextView)findViewById(R.id.log)).setText(a);
qNum++;
String orange = "SCORE = " + score;
String banana = "Q# " + qNum;
((TextView)findViewById(R.id.score)).setText(orange);
((TextView)findViewById(R.id.qNum)).setText(banana);
ask();
}
}
GAME:
public class Game {
public String capitalanswer = "";
public String countryanswer = "";
private CountryDB db;
public Game(){
this.db = new CountryDB();
}
public String qa(){
List<String> countries = db.getCapitals();
int n = countries.size();
int index = (int)( n * Math.random());
String c = countries.get(index);
capitalanswer = c;
System.out.println(capitalanswer);
Map<String, Country> data = db.getData();
Country ref = data.get(c);
countryanswer = ref.toString();
System.out.println(countryanswer);
if (Math.random() < 0.5){
return "What country has the capital " + ref.getCapital() + " ?" + "n" + ref.getName();
}else{
return "What is the capital of " + ref.getName() + " ?" + "n" + ref.getCapital();
}
}
}
Any help is appreciated!
java
marked as duplicate by Andy Turner
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 22 at 20:05
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
1
Game is never instantiated
– Andrei Dumitrescu-Tudor
Nov 22 at 20:05
add a comment |
This question already has an answer here:
What is a NullPointerException, and how do I fix it?
12 answers
I'm getting this error "Attempt to invoke virtual method 'java.lang.String eecs1022.caps.Game.qa()' on a null object reference"
I just want to know why im getting this error and how I can work around it.
The game object is returns a string but for some reason it says its a null object. Here is my main class and my game class.
MAIN:
private Game game;
private String question = "";
private String answer = "";
private int score = 1;
private int qNum = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ask();
}
public void ask(){
String apple = game.qa();
int indexanswer = apple.indexOf("n");
answer = apple.substring(indexanswer);
question = apple.substring(0,indexanswer);
((TextView)findViewById(R.id.question)).setText(question);
}
public void onDone (View v){
if (qNum == 10){
finish();
}
String answerinput = ((EditText)findViewById(R.id.answer)).toString();
if (answer.equalsIgnoreCase(answerinput)){
score++;
}
String oldlog = ((TextView)findViewById(R.id.log)).toString();
String a = oldlog + "n" + "Q# " + qNum + ": " + question + "n" + "Your answer" + answerinput.toUpperCase() + "n" + "Correct Answer: " + answer + "n";
((TextView)findViewById(R.id.log)).setText(a);
qNum++;
String orange = "SCORE = " + score;
String banana = "Q# " + qNum;
((TextView)findViewById(R.id.score)).setText(orange);
((TextView)findViewById(R.id.qNum)).setText(banana);
ask();
}
}
GAME:
public class Game {
public String capitalanswer = "";
public String countryanswer = "";
private CountryDB db;
public Game(){
this.db = new CountryDB();
}
public String qa(){
List<String> countries = db.getCapitals();
int n = countries.size();
int index = (int)( n * Math.random());
String c = countries.get(index);
capitalanswer = c;
System.out.println(capitalanswer);
Map<String, Country> data = db.getData();
Country ref = data.get(c);
countryanswer = ref.toString();
System.out.println(countryanswer);
if (Math.random() < 0.5){
return "What country has the capital " + ref.getCapital() + " ?" + "n" + ref.getName();
}else{
return "What is the capital of " + ref.getName() + " ?" + "n" + ref.getCapital();
}
}
}
Any help is appreciated!
java
This question already has an answer here:
What is a NullPointerException, and how do I fix it?
12 answers
I'm getting this error "Attempt to invoke virtual method 'java.lang.String eecs1022.caps.Game.qa()' on a null object reference"
I just want to know why im getting this error and how I can work around it.
The game object is returns a string but for some reason it says its a null object. Here is my main class and my game class.
MAIN:
private Game game;
private String question = "";
private String answer = "";
private int score = 1;
private int qNum = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ask();
}
public void ask(){
String apple = game.qa();
int indexanswer = apple.indexOf("n");
answer = apple.substring(indexanswer);
question = apple.substring(0,indexanswer);
((TextView)findViewById(R.id.question)).setText(question);
}
public void onDone (View v){
if (qNum == 10){
finish();
}
String answerinput = ((EditText)findViewById(R.id.answer)).toString();
if (answer.equalsIgnoreCase(answerinput)){
score++;
}
String oldlog = ((TextView)findViewById(R.id.log)).toString();
String a = oldlog + "n" + "Q# " + qNum + ": " + question + "n" + "Your answer" + answerinput.toUpperCase() + "n" + "Correct Answer: " + answer + "n";
((TextView)findViewById(R.id.log)).setText(a);
qNum++;
String orange = "SCORE = " + score;
String banana = "Q# " + qNum;
((TextView)findViewById(R.id.score)).setText(orange);
((TextView)findViewById(R.id.qNum)).setText(banana);
ask();
}
}
GAME:
public class Game {
public String capitalanswer = "";
public String countryanswer = "";
private CountryDB db;
public Game(){
this.db = new CountryDB();
}
public String qa(){
List<String> countries = db.getCapitals();
int n = countries.size();
int index = (int)( n * Math.random());
String c = countries.get(index);
capitalanswer = c;
System.out.println(capitalanswer);
Map<String, Country> data = db.getData();
Country ref = data.get(c);
countryanswer = ref.toString();
System.out.println(countryanswer);
if (Math.random() < 0.5){
return "What country has the capital " + ref.getCapital() + " ?" + "n" + ref.getName();
}else{
return "What is the capital of " + ref.getName() + " ?" + "n" + ref.getCapital();
}
}
}
Any help is appreciated!
This question already has an answer here:
What is a NullPointerException, and how do I fix it?
12 answers
java
java
asked Nov 22 at 20:01
Daniel Ilie
52
52
marked as duplicate by Andy Turner
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 22 at 20:05
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by Andy Turner
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 22 at 20:05
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
1
Game is never instantiated
– Andrei Dumitrescu-Tudor
Nov 22 at 20:05
add a comment |
1
Game is never instantiated
– Andrei Dumitrescu-Tudor
Nov 22 at 20:05
1
1
Game is never instantiated
– Andrei Dumitrescu-Tudor
Nov 22 at 20:05
Game is never instantiated
– Andrei Dumitrescu-Tudor
Nov 22 at 20:05
add a comment |
1 Answer
1
active
oldest
votes
By doing this:
private Game game;
You declare the variable game
, but you don't actually initialise it. That tells the compiler that you will be using game
to refer to a variable whose type is Game
. However, you don't actually give a value to game
. That is why game
is null
.
To initialise game
, you should be doing this instead:
private Game game = new Game();
Oh wow, i've been looking for a mistake for so long and I cant believe I missed that. Thank u!
– Daniel Ilie
Nov 22 at 20:07
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
By doing this:
private Game game;
You declare the variable game
, but you don't actually initialise it. That tells the compiler that you will be using game
to refer to a variable whose type is Game
. However, you don't actually give a value to game
. That is why game
is null
.
To initialise game
, you should be doing this instead:
private Game game = new Game();
Oh wow, i've been looking for a mistake for so long and I cant believe I missed that. Thank u!
– Daniel Ilie
Nov 22 at 20:07
add a comment |
By doing this:
private Game game;
You declare the variable game
, but you don't actually initialise it. That tells the compiler that you will be using game
to refer to a variable whose type is Game
. However, you don't actually give a value to game
. That is why game
is null
.
To initialise game
, you should be doing this instead:
private Game game = new Game();
Oh wow, i've been looking for a mistake for so long and I cant believe I missed that. Thank u!
– Daniel Ilie
Nov 22 at 20:07
add a comment |
By doing this:
private Game game;
You declare the variable game
, but you don't actually initialise it. That tells the compiler that you will be using game
to refer to a variable whose type is Game
. However, you don't actually give a value to game
. That is why game
is null
.
To initialise game
, you should be doing this instead:
private Game game = new Game();
By doing this:
private Game game;
You declare the variable game
, but you don't actually initialise it. That tells the compiler that you will be using game
to refer to a variable whose type is Game
. However, you don't actually give a value to game
. That is why game
is null
.
To initialise game
, you should be doing this instead:
private Game game = new Game();
edited Nov 22 at 20:08
answered Nov 22 at 20:04
pavlos163
481743
481743
Oh wow, i've been looking for a mistake for so long and I cant believe I missed that. Thank u!
– Daniel Ilie
Nov 22 at 20:07
add a comment |
Oh wow, i've been looking for a mistake for so long and I cant believe I missed that. Thank u!
– Daniel Ilie
Nov 22 at 20:07
Oh wow, i've been looking for a mistake for so long and I cant believe I missed that. Thank u!
– Daniel Ilie
Nov 22 at 20:07
Oh wow, i've been looking for a mistake for so long and I cant believe I missed that. Thank u!
– Daniel Ilie
Nov 22 at 20:07
add a comment |
1
Game is never instantiated
– Andrei Dumitrescu-Tudor
Nov 22 at 20:05