Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

nullpointerexcption

Status
Not open for further replies.

m3the01

Technical User
Feb 21, 2002
41
CA
Been having this problem for a couple of days now, cant seem to figure it out. The error from what i can find over the internet has somethin to do with how the constructor is used.

example
//constructor
public Board(Color top,Color bottom,Color middle){

topRowColor = top;
bottomRowColor = bottom;
middleRowColor = middle;

}

but to me this seems fine, i am very novice java programmer created a few games and calculators nothing special. TopROwColor....... have already been declared by class Color and are also private
meaning
private Color topRowCOlor.............


just dont understand why this would give me an error, unless it has nothing to do witht the constructor which is what i am starting to think.

If that is the case when else would you get the nullpointerexception error?


thanks again for any help
 
It would have been extremely helpful if you had given us the whole error-message and the line where this has occured.

The only thing I can say from here is that you perhaps just haven't filled the Color-object with any value before calling the constructor.

bye

frag patrick.metz@epost.de
 
yeh i know i passed the color object red, yellow and blue. This is simply in another class. I will try to get you the whole code in this class.


thanks
 
here is the code,this is the board class



import java.util.*;
import java.io.*;
import java.awt.*;
import java.applet.Applet;

public class Board extends Applet {

// Constants
private static final Color emptySquareColor = Color.white;
private static final int defaultEmpty = 5;
private static final int boardSize = 3;

// instance variables
private Panel row1, row2, row3, boardPanel;
private Button square1, square2, square3, square4, square5;
private Button square6, square7, square8, square9;
private Color topRowColor, middleRowColor, bottomRowColor;
private int emptySquare;


// Constructor
public Board(Color top, Color middle, Color bottom) {


topRowColor = top;
middleRowColor = middle;
bottomRowColor = bottom;







}

// Instance methods
private Panel makeRowPanel( LayoutManager lm) {

Panel p = new Panel();
p.setLayout(new FlowLayout(FlowLayout.CENTER,4,1));
return p;





}

private Button makeSquare(String squareNumber) {
// Creates and returns a button with the given String as its Label
Button buttons = new Button(squareNumber);
buttons.setFont(new Font("Courier",Font.BOLD,10));
return buttons;






}

public void paintSquares( ) {
// Sets the colours of the squares according to their row colours
Color square2 = topRowColor;
Color square3 = topRowColor;
Color square4 = middleRowColor;
Color square5 = middleRowColor;
Color square6 = middleRowColor;
Color square7 = bottomRowColor;
Color square8 = bottomRowColor;
Color square9 = bottomRowColor;




}

private void fillRows( ) {
// Create the squares and their labels, add them to the appropriate rows 1, 2 or 3

row1 = makeRowPanel(new FlowLayout(FlowLayout.LEFT,4,2));
row1.add(square1 = makeSquare("1"));
row1.add(square2 = makeSquare("2"));
row1.add(square3 = makeSquare("3"));

row2 = makeRowPanel(new FlowLayout(FlowLayout.LEFT,4,2));
row2.add(square4 = makeSquare("4"));
row2.add(square5 = makeSquare("5"));
row2.add(square6 = makeSquare("6"));

row3 = makeRowPanel(new FlowLayout(FlowLayout.LEFT,4,2));
row3.add(square7 = makeSquare("7"));
row3.add(square8 = makeSquare("8"));
row3.add(square9 = makeSquare("9"));





}

private void assembleRows( ) {
// Arranges the rows into a single Panel (using GridLayout)

boardPanel = makeRowPanel(new GridLayout (3,1));
boardPanel.add(row1);
boardPanel.add(row2);
boardPanel.add(row3);


}

public Panel getBoard( ){
// Returns a reference to the Panel into which all 3 rows have been assembled.


return boardPanel;



}

} // end of Board class



this is the start of the main class,




import java.awt.*;
import java.applet.*;

public class BoardTest extends Applet {

public void init() {

this.part1BoardTest();
}

private void part1BoardTest() {

Board board;
board = new Board(Color.red, Color.yellow, Color.blue);
Panel myBoardPanel = new Panel(new FlowLayout());
myBoardPanel.add(board.getBoard());
this.add(myBoardPanel);
}
}



here is the error

java.lang.nullpointerexception


 
please no slack from anyone, just starting out programming.


thanks
 
Looks like boardPanel gets created in assembleRows() but
I don't see anywhere that that is getting called.
The null pointer exception is probubly being thrown
by the getBoard method.

Catch the exception and do a printStackTrace on it to
see the origin.
 
so in the getboard method i have to return a reference, where should i call the boardPanel? Should i just add(boardPanel);
or am i missing somethin


thanks for your time
 
I can't really speak to where you should do this just
that the boardPanel object has to be created before
it can be returned from getBoard. Perhaps if there
is more than one possible scenario the getBoard
method should check to see if boardPanel is null and
if so the call assembleRows to create if

if(boardPanel==null)
assembleRows();
return boardPanel;
 
well found out what the problem was, thanks for all the help
found out how to use methods inside of classes correctly.
thanks again
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top