My main class
Objective: Check a string value from another class so I can use that value to make calculations
Problem: String value is null on second click so it throws a nullpointer exception.
It's this line that gives me a nullpointer. Or any of the the .equals lines. It works the first time, honestly I don't know why... It says the value is null, but on the second mouse click it is set to null but doesn't work and error / crashes program.
Here is the class I'm using
Objective: Check a string value from another class so I can use that value to make calculations
Problem: String value is null on second click so it throws a nullpointer exception.
private void jLabel63MouseClicked(java.awt.event.MouseEvent evt) {
randomResource rg3 = new randomResource();
rg3.setResource(lumber63, brick63, coin63, grain63);
System.out.println(rg3.resourceValue);
if (!(discovered63 == true)) {
rg3.genNum(clicked63);
}
if (rg3.resourceValue.equals("lumber")) {
jLabel63.setIcon(new ImageIcon("src/hexTree.png"));
lumber63 = true;
discovered63 = true;
rg3.setResource(lumber63, brick63, coin63, grain63);
if (villagerAmount <= 0) {
jLabel63.setIcon(new ImageIcon("src/hexTreeRedFull.png"));
}
if (villagerAmount >= 1) {
jLabel63.setIcon(new ImageIcon("src/hexTreeCutting.png"));
harvestReady = true;
ActionListener taskPerformer5 = new ActionListener() {
public void actionPerformed(ActionEvent evt) {
while (buffRemaining >= 0) {
resource1Amount++;
buffRemaining--;
}
if (buffRemaining <= 0) {
harvesting = false;
}
}
};
new Timer(delayThreeTwo, taskPerformer5).start();
villagerAmount--;
villagers.setText("Villagers: " + villagerAmount);
}
}
if (rg3.resourceValue.equals("bricks")) {
jLabel63.setIcon(new ImageIcon("src/hexRock.png"));
discovered63 = true;
brick63 = true;
rg3.setResource(lumber63, brick63, coin63, grain63);
}
if (rg3.resourceValue.equals("grain")) {
jLabel63.setIcon(new ImageIcon("src/defaultFood.png"));
grain63 = true;
discovered63 = true;
rg3.setResource(lumber63, brick63, coin63, grain63);
}
if (rg3.resourceValue.equals("coin")) {
jLabel63.setIcon(new ImageIcon("src/hexCoin.png"));
coin63 = true;
discovered63 = true;
rg3.setResource(lumber63, brick63, coin63, grain63);
} else {
System.out.println("else, your program sucks. ");
discovered63 = true;
}
}
if (rg3.resourceValue.equals("lumber"))
It's this line that gives me a nullpointer. Or any of the the .equals lines. It works the first time, honestly I don't know why... It says the value is null, but on the second mouse click it is set to null but doesn't work and error / crashes program.
Here is the class I'm using
import java.util.Random;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Matthew
*/
public class randomResource {
PlayingField field = new PlayingField();
public boolean chosenResource;
public int randomResource;
public boolean discovered;
public boolean lumber;
public boolean grain;
public boolean coin;
public boolean brick;
public String resourceValue;
public String genNum(boolean chosen) {
resourceValue = "";
Random rg = new Random();
if (!(chosenResource == true)) {
randomResource = rg.nextInt(99) + 1;
}
if (randomResource >= 1 && randomResource <= 25) {
lumber = true;
chosenResource = true;
resourceValue += "lumber";
}
if (randomResource >= 25 && randomResource <= 50) {
brick = true;
chosenResource = true;
resourceValue += "bricks";
}
if (randomResource >= 50 && randomResource <= 75) {
grain = true;
chosenResource = true;
resourceValue += "grain";
}
if (randomResource >= 75 && randomResource <= 100) {
coin = true;
chosenResource = true;
resourceValue += "coin";
} else {
}
return resourceValue;
}