I am working a program that runs through a JFrame I have created and I am just having a few problems calling on certain methods in my code. The issues I am running a method that outputs something based upon evaluation, I keep getting nullpointers at the same spots.
JButton btnDealCards = new JButton("Deal Cards");
btnDealCards.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
displayYourHand.setText("");
output = "";
couples = 0;
for (int i = 0; i < hand.length; i++) {
Card1 dealt = dealHand();
if (dealt != null) {
hand[i] = dealt;
displayYourHand.setText(displayYourHand.getText()
+ hand[i].toString() + "\n");
} else {
displayYourHand.setText("NOT ENOUGH CARDS TO DEAL");
status.setText("Shuffle cards to continue");
return;
}
}
// totalHand();
// pairs();
// twoPair();
// threeOfAKind();
}
});
btnDealCards.setBounds(336, 192, 98, 26);
contentPane.add(btnDealCards);
JButton btnShuffleCards = new JButton("Shuffle Cards");
btnShuffleCards.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
shuffle();
displayYourHand.setText("The Deck Has Been Shuffled");
}
});
btnShuffleCards.setBounds(314, 229, 147, 23);
contentPane.add(btnShuffleCards);
}
public void shuffle() {
for (int first = 0; first < deck.length; first++) {
int second = randomNumbers.nextInt(52);
Card1 temp = deck[first];
deck[first] = deck[second];
deck[second] = temp;
}
btnDealCards.setEnabled(true);
}
public Card1 dealHand() {
if (currentCard < deck.length)
return deck[currentCard++];
else {
btnDealCards.setEnabled(false);
return null;
}
}
public void pairs() {
for (int k = 0; k < faces.length; k++)
if (numbers[k] == 2) {
output += "" + ("Pair of " + faces[k] + "'s ");
couples++;
}
status.setText(output);
}
The first is my actionlistener and actionperformer and the next three are the actions I wish to preform that spit out the nullpointers. The issues are always the lines after the for loop such as the "status.setText(output);" or "btnDealCards.setEnabled(true);". I am thinking I need to turn these statements into return statements but that is my only idea. Thanks in advance!
The variables that are being dereferenced on the lines that are throwing the NullPointerException (NPE) are null. For instance, the status variable is null if you're getting a NPE on this line:
status.setText("Shuffle cards to continue");
and the displayYourHand variable is null if this line throws a NPE:
displayYourHand.setText("The Deck Has Been Shuffled");
and likewise the btnDealCards variable is null if this line throws a NPE:
btnDealCards.setEnabled(true);
My bet: you may be shadowing the variable by re-declaring it in a constructor or init method when you try to create the object, leaving the class field null. The key will be to look at the code where you think that you're initializing these variables and see why you're not in fact initializing them.
Related
I am trying to pass an element from jlb[] to the method new1(JLabel jl). Here is my code:
JLabel jlb[]=new JLabel[10];
for (int i = 0; i < 10; i++) {
jlb[i]=new JLabel("jlbl"+i);
}
new1(<variable name>);
new1 is a method. It has a JLabel parameter. I want call new1(). What should I place instead of <variable name> to call the method with variable from the array?
This is new1 method:
void new1(final JLabel jlbl){
new Thread(){
#Override
public void run() {
int y=10;
while (b>=150) {
for (int b=300; b > 150; b--) {
try {
Thread.sleep(10);
jlbl.setLocation(b, y);
} catch (Exception e) {
}
}
}
jLabel1.setLocation(b, y);
}
}.start();
}
As I understand you are looking for a alphanumerical token that would identify your variable like myJLabel or cuteLabelFromAnArray. Well, data stored in arrays can be accessed with special operator []. In your example this would be for example:
new1(jlb[<put index here>]);
I'm making a math flashcard application that takes 20 answers and randomizes the question each time.
My Problem: The for loop does not wait until the button is pressed. I need to find a way to wait for the ActionListener to be executed before it goes through the loop again.
Please bear with me as I am quite new to Java.
Here is my current code:
for(int i = 0; i < 20; i++){
Random numberOne = new Random();
Random numberTwo = new Random();
firstNumberInt = numberOne.nextInt(12) + 1;
secondNumberInt = numberTwo.nextInt(12) + 1;
firstNumber = Integer.toString(firstNumberInt);
secondNumber = Integer.toString(secondNumberInt);
firstNumberPrint.setText(firstNumber);
secondNumberPrint.setText(secondNumber);
answerBtn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
String answerString = answerField.getText();
try {
int answer = Integer.parseInt(answerString);
} catch(NumberFormatException e){
error.setText("Number Cannot Be A Word");
}
}
});
}
The ActionListener should be outside the loop and the loop should be within it.
Not the opposite.
answerBtn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
// Loop somewhere inside here
String answerString = answerField.getText();
try {
int answer = Integer.parseInt(answerString);
} catch(NumberFormatException e){
error.setText("Number Cannot Be A Word");
}
}
});
The code inside the overridden method inside the ActionListener class is being invoked every time the Component is interacted.
This class is in a program for a game I'm writing that is basically Space Invaders. I'm getting Exceptions for some reason and I don't see why I should be getting them.
Here's the class in question, but I can post all the code if necessary:
public class GamePanel extends JPanel {
Launcher launcher1;
Background bground1;
Shot shot;
public ArrayList<Shot> shots;
public int numShots;
public static int counter;
public GamePanel() throws IOException {
super();
this.shots = new ArrayList<>();
this.numShots = 0;
launcher1 = new Launcher();
bground1 = new Background();
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(bground1.background, 0, 0, getWidth(), getHeight(), null);
g.drawImage(launcher1.baldEagleImage, launcher1.getLxCoord(), launcher1.lyCoord, null);//paint the launcher
while (counter == 1) {
for (int i = 0; i < shots.size(); i++) {
g.drawImage(shots.get(i).mcDShotImage, shots.get(i).staticXLauncherCoord, shots.get(i).getSyCoord(), null);
}
}
}
public void move(GamePanel gamePanel) {
launcher1.moveX();
if (numShots > 0) {
moveShot();
}
repaint();
}
public void moveShot() {
for (int i = 0; i < numShots; i++) {//loop to move all the shots
if (shots.get(i).getSyCoord() > 10) { // THIS IS THE ISSUE, but I don't know why
counter = 1;
shots.get(i).moveY();
repaint();
} else {
counter = 0;
shots.remove(i);
repaint();
}
}
}
public void createShots() {
try {
for (int j = 0; j < numShots; j++) {
shots.add(new Shot());
}
} catch (IOException | IndexOutOfBoundsException e) {
System.out.println("caught an exception" + e);
}
}
}
The problem is in this piece of code:
} else {
counter = 0;
shots.remove(i);
repaint();
}
You remove an item from shots without adjusting numShots, causing an index out of bounds exception in one of subsequent iterations.
To fix this, either add numShots-- in the else branch, or use the built-in size() method that returns the count of elements in a list instead: unlike numShots which you need to maintain, shots.size() never gets "out of sync" with the actual count.
In the above line of code what is apparent is the issue is numShots is > shots.size() (because you also remove shots.
Since I couldn't where you are incrementing the numShots, in this piece of code one simple (albeit not sure from your logic perspective) change your for loop as below:
for (int i = 0; i < shots.size(); i++)
The following part of the code doesn't work, as the won/lost count keeps incrementing by more than 1 for each word, and sometimes I get a nullpointerexception with the string length. Moreover, although the player is supposed to get 7 tries(int no), sometimes he gets more, sometimes less. The Strings are taken from a text file "Hangeng.txt". The whole game is inside a keyboard keytyped listener that is inside a button listener. Any tips on how the layout of the game should generally be arranged so as to avoid errors are welcome, as I am only beginning to work with swing and gui stuff.
public class test{
static int won = 0;
static int lost = 0;
static String key = "";
static String word = null;
static int no = 0;
static StringBuffer toguess;
public static void main(String[] args) throws IOException{
JFrame frame = new JFrame();
frame.setLayout(new GridLayout(3,1));
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
JPanel panel3 = new JPanel();
JButton button = new JButton();
JLabel label = new JLabel();
JLabel label2 = new JLabel();
panel1.add(label);
panel2.add(button);
panel3.add(label2);
frame.setSize(800,600);
frame.add(panel1);
frame.add(panel2);
frame.add(panel3);
frame.setVisible(true);
//the button that starts the game or gets a new word
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
frame.requestFocus();
no = 0;
label2.setText("won " + won + ", lost " + lost);
button.setText("Next");
//get random word from file
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(
"hangeng.txt"));
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
int lineno = (int) (Math.random() * 100);
for (int i = 0; i < lineno; i++) {
try {
reader.readLine();
} catch (IOException e1) {
e1.printStackTrace();
}
}
try {
word = reader.readLine().replace(" ", "");
} catch (IOException e1) {
e1.printStackTrace();
}
String missing = "";
for (int u = 0; u < (word.length() - 2); u++) {
missing = missing + "*";
}
final String guess = word.charAt(0) + missing
+ word.charAt((word.length() - 1));
toguess = new StringBuffer(guess);
label.setText(toguess.toString());
final ArrayList<String> tried = new ArrayList<String>();
//keylistener that listens to key clicks by the user
frame.addKeyListener(new KeyListener() {
public void keyPressed(KeyEvent arg0) {
}
public void keyReleased(KeyEvent arg0) {
}
public void keyTyped(KeyEvent arg0) {
key = "" + arg0.getKeyChar();
String guessing = null;
boolean k = false;
if ((no < 6)) {
guessing = key;
System.out.println(guessing);
if (!(tried.contains(guessing))) {
tried.add(guessing);
for (int length = 1; length < (guess
.length() - 1); length++) {
if (guessing.equals(String.valueOf(word.charAt(length)))) {
toguess.replace(length,
(length + 1),
String.valueOf(word.charAt(length)));
k = true;
}
}
if (k == true) {
label.setText(toguess.toString());
} else {
no = no + 1;
}
k = false;
}
label.setText(toguess.toString());
if (toguess.toString().equals(word)) {
label.setText("Correct! The word was " + word);
no = 6;
won = won + 1;
}
}
else if ((no == 6)
&& (!(toguess.toString().equals(word)))) {
label.setText("Sorry, but the word was " + word);
lost = lost + 1;
}
}
});
}
});
}
}
+1 to all comments....
Adding to them:
Do not use KeyListener use a KeyAdapter however as you are using Swing and not AWT you should use KeyBindings for Swing see here for example.
Dont forget to create and manipulate Swing components on Event Dispatch Thread via SwingUtiltities.invokeLater(..) block see here for more.
Check class naming schemes they shuld start with capital letter, i.e test should be Test and every new word after that should be capitalized.
Do not call setSize on JFrame rather use appropriate LayoutManager and/or override getPreferredSize() of JPanel and return a size which fits its content and call pack() on JFrame instance after adding all components.
Also SSCCE should be compilable from copy and paste this is not.... i.e variables needed to be changed to final and I dont have a sample of Hangeng.txt so cant test
Lastly use the #Override annotation to ensure you are overriding the correct methods, i.e
#Override
public void actionPerformed(ActionEvent e) {
}
I wrote the code below. It checks input from a JTextField and ensures the user is typing in numbers. If not the box flashes red and the invalid character is removed.
The tipArray[] is an array of JTextFields that I add to the JFrame with a loop.
How can I apply the following code to every possible array (tipArray[0], tipArray[1] ....tipArray[6])?
tipArray[6].addKeyListener(new KeyAdapter() {
#Override
public void keyPressed(KeyEvent e) {
char keyChar = e.getKeyChar();;
char[] badCharArray = "abcdefghijklmnopqrstuvwxyz-`~!##$%^&*()[,]{}<>_+=|\"':;?/ ".toCharArray();
for (int i = 0; i < badCharArray.length; i++) {
if (badCharArray[i] == keyChar) {
tipArray[1].setBackground(Color.RED);
}
}
}
#Override
public void keyReleased(KeyEvent e) {
if (tipArray[6].getBackground() == Color.RED) {
if (tipArray[6].getText() != "0"){
String removeLastLetter = tipArray[1].getText().substring(0, tipArray[6].getText().length()-1);
tipArray[6].setText(removeLastLetter);
tipArray[6].setBackground(Color.WHITE);
}
}
}
});
The loops I have tried dont work:
for (int i = 0; i <= 6; i++) {
tipArray[i].addKeyListener(new KeyAdapter() {
#Override
public void keyPressed(KeyEvent e) {
char keyChar = e.getKeyChar();;
char[] badCharArray = "abcdefghijklmnopqrstuvwxyz-`~!##$%^&*()[,]{}<>_+=|\"':;?/ ".toCharArray();
for (int x = 0; x < badCharArray.length; x++) {
if (badCharArray[x] == keyChar) {
tipArray[i].setBackground(Color.RED);
}
}
}
#Override
public void keyReleased(KeyEvent e) {
if (tipArray[i].getBackground() == Color.RED) {
if (tipArray[i].getText() != "0"){
String removeLastLetter = tipArray[i].getText().substring(0, tipArray[i].getText().length()-1);
tipArray[i].setText(removeLastLetter);
tipArray[i].setBackground(Color.WHITE);
}
}
}
});
}
^The above results in all of the variable i's after line "if (badCharArray[x] == keyChar) {" having a syntax error.
Change your counter in the for loop in the second one to be a different variable (z instead of i perhaps). You have a duplicate variable the way it is now (two i's). Also, it is recommended you use a DocumentListener, not KeyListener, for checking for invalid characters, as KeyListeners sometimes fail.