File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes Java Messages Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Java Messages" Watch "Java Messages" New topic

Java Messages

jonas okwara
Ranch Hand

Joined: Jun 22, 2004
Posts: 58
My program compiles fine but spews out these error messages below.When I run it. I have been trying to figure out the problems without success. Please I will appreciate any help


Exception in thread "main" java.lang.NullPointerException
at QuestionBox.<init>(
at QuestionBox.main(
Joe Ess

Joined: Oct 29, 2001
Posts: 9189

Sounds like you have a null pointer at line 33. If you need more help, feel free to show us the relevant code.

[How To Ask Questions On JavaRanch]
jonas okwara
Ranch Hand

Joined: Jun 22, 2004
Posts: 58
//Here is the code:

import java.text.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.border.*;

//The beginning of second frame

class QuestionBox extends JFrame implements ActionListener, DocumentListener {

private String candidateName;
private int fileNumber = 0;
private String[] myFiles = { "file1.txt", "file2.txt", "file3.txt", "file4.txt", "file5.txt"};
JPanel candidatePanel,textPanel,buttonPanel;
JLabel candidateLabel;
JTextField candidateTextField;
JButton startButton, cancelButton;

Font font = new Font ("New Courier", Font.PLAIN, 17);
//RealT real;
super("Second Frame");

// Set up CandidateLabel
candidateLabel = new JLabel();
candidateLabel.setFont( font );
candidatePanel.add( candidateLabel);

//set up candidateTextField
candidateTextField = new JTextField( 25 );
candidateTextField.setPreferredSize(new Dimension(750, 45));

// set up startButton
startButton = new JButton();
startButton.setPreferredSize(new Dimension(180, 40));
startButton.setBorder( new BevelBorder(BevelBorder.RAISED));
startButton.setFont( font);
startButton.setEnabled( true);
buttonPanel.add( startButton);
startButton.addActionListener( this );

//set up cancelButton
cancelButton = new JButton();
cancelButton.setPreferredSize(new Dimension(180, 40));
cancelButton.setBorder( new BevelBorder(BevelBorder.RAISED));
cancelButton.setFont( font);
cancelButton.setText( "Cancel" );
cancelButton.addActionListener( this );

Container contentPane = getContentPane();
buttonPanel = new JPanel();
buttonPanel.setLayout(new FlowLayout( FlowLayout.CENTER));
textPanel.setLayout(new FlowLayout( FlowLayout.CENTER));
contentPane.add(buttonPanel, BorderLayout.SOUTH);
contentPane.add(candidatePanel, BorderLayout.CENTER);
contentPane.add(textPanel, BorderLayout.NORTH);
setBounds(10, 10, 600, 300);

public void insertUpdate(DocumentEvent evt){
if ( evt.getDocument() == candidateTextField.getDocument()){
//candidateName == candidateTextField.getDocument();
public void removeUpdate(DocumentEvent evt){}
public void changedUpdate( DocumentEvent evt ){}
public void actionPerformed( ActionEvent evt ){
String actCmd = evt.getActionCommand();

//Start The test
if ( actCmd.equals ("Begin_Exam")){
if (actCmd.equals("Cancel")){


public static void main( String args[]){
new QuestionBox();

jonas okwara
Ranch Hand

Joined: Jun 22, 2004
Posts: 58
Still waiting for the reply
Joe Ess

Joined: Oct 29, 2001
Posts: 9189

Originally posted by jonas okwara:
Still waiting for the reply

Ya see, we're all volunteers here, answering these questions in our free time, so you may experience some delays.
Now, as for your problem, ya got two variables on line 33, candidatePanel and candidateLabel. You are getting a null pointer exception, which means that something on line 33 is null. So ask yerself: "where do I initalize those two variables".
[ July 01, 2004: Message edited by: Joe Ess ]
jonas okwara
Ranch Hand

Joined: Jun 22, 2004
Posts: 58
Thanks a lot joe ess

[Removed unessecary insult - please watch this in the future ]
[ July 01, 2004: Message edited by: Gregg Bolinger ]
I agree. Here's the link:
subject: Java Messages
jQuery in Action, 3rd edition