Archived

This topic is now archived and is closed to further replies.

Bdiddy

[java] what's wrong?!?!

Recommended Posts

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[i] = 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[i] = FeudGame.MakeGame(Game.standardNum, feud.games[i]); } } Source/

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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[i] = 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[i] = FeudGame.MakeGame(Game.standardNum, feud.games[i]);

}

}










Source/

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites