[java] what's wrong?!?!

Started by
3 comments, last by Bdiddy 22 years, 5 months ago
I keep getting a null pointer exception and it just isn''t acting correctly Please someone help me fix it... thanks /Source import java.awt.*; import java.util.*; import java.awt.event.*; import javax.swing.*; import javax.swing.tree.*; import java.io.*; class Game { public static final int standardNum = 10; public String qAndA[]; // This array holds the question and the answer public String points[]; public static int gameNum; // Keeps track of the total number of games public int myNum; // particular number for this game public int numAns; // the number of answers for this game } //4a1e62 purple //A6870D gold public class FeudGame extends JFrame { // array of games public Game games[]; // interface JTextField ansSlots[], question; JPanel mainPanel, secPanel; JButton reset; /* This is the constructor of the main class, FeudGame it will be passed the number of games */ //The interface is implemented here... public FeudGame(int numGames) { games = new Game[numGames]; //games[0].qAndA = new String[Game.standardNum]; mainPanel = new JPanel(); secPanel = new JPanel(); question = new JTextField(20); ansSlots = new JTextField[10]; getContentPane().add(mainPanel, BorderLayout.CENTER); getContentPane().add(secPanel, BorderLayout.SOUTH); reset = new JButton("RESET"); secPanel.add(question); secPanel.add(reset); } /*Later on this function will be placed in a for loop so that we may manually enter the data for each game*/ public static Game MakeGame(int numberOfAns, Game g) throws IOException { BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter the question"); /*g.qAndA[0]*/ String ques = input.readLine(); System.out.println("3"); /*g.points[0]*/ g.qAndA[0] = g.points[0]= ques; System.out.println(g.qAndA[0]); for( int i = 1; i < numberOfAns; i++) { System.out.println("Enter the answer" + i + "to the question"); String answers = input.readLine(); g.qAndA = answers; System.out.println("Enter the number of points"); String pts = input.readLine(); g.points = pts; } g.myNum = ++Game.gameNum; return g; } public static void main(String Args[]) { FeudGame feud = new FeudGame(1); System.out.println("4"); try { feud.games[0] = FeudGame.MakeGame(2, new Game()); } catch(Exception e) {} //System.out.println(feud.games[0].qAndA[0]); // for(int i=0; i < 14; i++) // feud.games = FeudGame.MakeGame(Game.standardNum, feud.games); } } Source/ </i>
Advertisement
You're getting the null pointer exception on the line:

g.qAndA[0] = g.points[0]= ques;

At this point, g.points hasn't been initialized. It needs to have a g.points = new String[10], for example, to be used.

g.qAndA needs to be initialized too - which you have done but have commented out earlier in the code.

Hope that helps!

Michael

Edited by - mjacobsca on November 13, 2001 2:07:48 AM
I added the new stuff and I still get a null pointer exception...

/Source
import java.awt.*;
import java.util.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.tree.*;
import java.io.*;

class Game {


public static final int standardNum = 10;
public String qAndA[]; // This array holds the question and the answer
public String points[];
public static int gameNum; // Keeps track of the total number of games
public int myNum; // particular number for this game
public int numAns; // the number of answers for this game



}


//4a1e62 purple
//A6870D gold
public class FeudGame extends JFrame
{

// array of games
public Game games[];

// interface
JTextField ansSlots[], question;
JPanel mainPanel, secPanel;
JButton reset;


/* This is the constructor of the main class, FeudGame
it will be passed the number of games */
//The interface is implemented here...
public FeudGame(int numGames)
{

games = new Game[numGames];
games[0].qAndA = new String[Game.standardNum];
games[0].points = new String[Game.standardNum];
mainPanel = new JPanel();
secPanel = new JPanel();
question = new JTextField(20);
ansSlots = new JTextField[10];
getContentPane().add(mainPanel, BorderLayout.CENTER);
getContentPane().add(secPanel, BorderLayout.SOUTH);
reset = new JButton("RESET");
secPanel.add(question);
secPanel.add(reset);

}
/*Later on this function will be placed in a for loop so that we may manually enter
the data for each game*/
public static Game MakeGame(int numberOfAns, Game g) throws IOException
{
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the question");
/*g.qAndA[0]*/ String ques = input.readLine();
System.out.println("3");
/*g.points[0]*/ g.qAndA[0] = g.points[0]= ques;
System.out.println(g.qAndA[0]);

for( int i = 1; i < numberOfAns; i++)
{
System.out.println("Enter the answer" + i + "to the question");
String answers = input.readLine();
g.qAndA = answers;

System.out.println("Enter the number of points");
String pts = input.readLine();
g.points = pts;<br> <br><br> }<br> g.myNum = ++Game.gameNum;<br> return g;<br> }<br> <br> public static void main(String Args[])<br> {<br> FeudGame feud = new FeudGame(1);<br> System.out.println("4");<br> try { <br> feud.games[0] = FeudGame.MakeGame(2, new Game());<br> } catch(Exception e) {}<br> //System.out.println(feud.games[0].qAndA[0]);<br> <br> <br> <br> // for(int i=0; i < 14; i++)<br> // feud.games = FeudGame.MakeGame(Game.standardNum, feud.games);<br> <br> } <br><br>}<br><br><br><br><br><br><br> <br> <br><br> <br>Source/ </i>
You should invest in a line by line debugger to help find your errors. In this case, you are getting the error on the lines:

games[0].qAndA = new String[Game.standardNum];
games[0].points = new String[Game.standardNum];

You have not initialized games[0]. It is still null, so you can''t access qanda or points. You have:

games = new Game[numGames];

All this does is create space for the array. You need to then have a line such as:

games[0] = new Game()

to do the initialization.
One more thing I noticed, is that you are not correct in your use of the static argument. When you define a variable as being static you should initialize it as such:
    class Foo{ public int bob; public static final int standardNum; static{  standardNum = 10; }  Foo(){   bob = standardNum; }}  


Once you initialize that static variable you should never change it. Static variables are setup just once when the class is initially imported. So say for instance that you wanted to precalculate an array of sinus and cosinus values these would be perfect for a static variable. The calculations would be done only once when your program loaded no matter how different objects of that type you made, they would be done only once inside the program. And if they change somewhere else in your class they can be unreliable.

The only reason why I mention this is because I see a static variable, GameNum, constantly changing within your code. If you plan on it constantly changing, then by definition it is not a static variable and you could run into problems in the future.

Good luck making the game!

Edited by - wrathnut on November 14, 2001 9:25:48 AM

This topic is closed to new replies.

Advertisement