Attempt to invoke virtual method '' on a null object reference [duplicate]





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







-1
















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!










share|improve this question













marked as duplicate by Andy Turner java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

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 '18 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 '18 at 20:05


















-1
















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!










share|improve this question













marked as duplicate by Andy Turner java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

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 '18 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 '18 at 20:05














-1












-1








-1


0







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!










share|improve this question















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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 22 '18 at 20:01









Daniel IlieDaniel Ilie

52




52




marked as duplicate by Andy Turner java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

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 '18 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 java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

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 '18 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 '18 at 20:05














  • 1





    Game is never instantiated

    – Andrei Dumitrescu-Tudor
    Nov 22 '18 at 20:05








1




1





Game is never instantiated

– Andrei Dumitrescu-Tudor
Nov 22 '18 at 20:05





Game is never instantiated

– Andrei Dumitrescu-Tudor
Nov 22 '18 at 20:05












1 Answer
1






active

oldest

votes


















-1














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();






share|improve this answer


























  • 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 '18 at 20:07


















1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









-1














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();






share|improve this answer


























  • 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 '18 at 20:07
















-1














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();






share|improve this answer


























  • 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 '18 at 20:07














-1












-1








-1







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();






share|improve this answer















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();







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 22 '18 at 20:08

























answered Nov 22 '18 at 20:04









pavlos163pavlos163

5591047




5591047













  • 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 '18 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 '18 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 '18 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 '18 at 20:07





Popular posts from this blog

How to change which sound is reproduced for terminal bell?

Can I use Tabulator js library in my java Spring + Thymeleaf project?

Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents