Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


Exception in thread "main" problem please help

Posted on 2003-03-08
Medium Priority
Last Modified: 2010-04-17
I have written a problem in java, which compiles successfully, but I receive a message stating:
"Exception in thread "main" java.lang.NullPointerException
at Project6.list_Frame.<init><list_Test.java:138>
at Project6.list_Test.main<list_Test.java:185>"

I need some help. I asked my professor in java and he didn't understand the problem. Here is my source codes for the program. Please anyone help me out, the deadline is in four days.

source codes:
//Programmer: Bernardo Arocho Jr.

package Project6;

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

class Terminator extends WindowAdapter
  public void windowClosing( WindowEvent e )
    System.exit( 0 );
 }//end of Terminator
class TextPanel extends JPanel
  private String text;

    public TextPanel()
      set( "Choose a word on the left and find the synonym on the left" );
    public void set( String w )
      text = w;
    public void paintComponent( Graphics g )
      super.paintComponent( g );

      Font f = new Font( "Helvetica", Font.BOLD, 15 );
      g.setFont( f );
      g.setColor( Color.blue );

        g.drawString( text, 50, 100 );
 }//end of TextPanel
class list_Frame extends JFrame implements ListSelectionListener, ActionListener
  private TextPanel Tpanel;
  private JScrollPane scroll_word;
  private JScrollPane scroll_synonym;
  private JList words_List;
  private JList synonym_List;
  private JPanel p_word;
  private JPanel p_synonym;
  private JPanel result_Panel;
  private JButton result_Button;

  private int right = 0;
  private int wrong = 0;

  public list_Frame()
    setTitle( "Synonyms Test" );
    setSize( 500, 500 );
    setLocation( 100, 200 );

    addWindowListener( new Terminator() );

    //Creating the word list
    String[] words = { "stones", "giant", "stole", "throw", "like", "warm" };

    words_List = new JList( words );
    scroll_word = new JScrollPane( words_List );

    //Setting the number of words to seen in the list
    words_List.setVisibleRowCount( 3 );

    //Limiting the selection to one item or one range of items
    words_List.setSelectionMode( ListSelectionModel.SINGLE_INTERVAL_SELECTION );

    //Adding scrollpanel to the word list
    p_word = new JPanel();
    p_word.add( scroll_word );

    //Creating borders for the word list
    Border border1 = BorderFactory.createRaisedBevelBorder();
    Border border2 = BorderFactory.createTitledBorder( border1, "Word List",
                                                       TitledBorder.BELOW_TOP );
      p_word.setBorder( border2 );
      words_List.addListSelectionListener( this );


    //Creating the synonym list
    String[] synonym = { "rocks", "took", "toss", "huge", "enjoy", "hot",
                         "gigantic", "work", "ill", "quickest" };

    synonym_List = new JList( synonym );
    scroll_synonym = new JScrollPane( synonym_List );

    //Setting the number of words to seen in the list
    synonym_List.setVisibleRowCount( 4 );

    //Limiting the selection to one item or one range of items
    synonym_List.setSelectionMode( ListSelectionModel.SINGLE_INTERVAL_SELECTION);

    //Adding scrollpanel to the synonym list
    p_synonym = new JPanel();
    p_synonym.add( scroll_synonym );

    //Creating borders for the synonym list
    Border border3 = BorderFactory.createEtchedBorder();
    Border border4 = BorderFactory.createTitledBorder( border3, "Synonym List",
                                                       TitledBorder.BELOW_TOP );
      p_synonym.setBorder( border4 );
      synonym_List.addListSelectionListener( this );

      Container c = getContentPane();
      c.add( p_word, "West" );
      c.add( p_synonym, "East" );


    //Creating text in the panel with borders
    Tpanel = new TextPanel();

    Border border5 = BorderFactory.createEtchedBorder();
    Border border6 = BorderFactory.createTitledBorder( border5, "Message",
                                                       TitledBorder.BELOW_TOP );
      Tpanel.setBorder( border6 );

      c.add( Tpanel, "Center" );

    //Creating the result button
    result_Button = new JButton( "Results" );
    result_Panel.add( result_Button );
    result_Button.addActionListener( this );

      getContentPane().add( result_Panel, "South" );

   }//end of list_Frame constructor


  public void valueChanged( ListSelectionEvent e )
    Tpanel.set( "" );
    String s = ( String )words_List.getSelectedValue();
    String s1 = ( String )synonym_List.getSelectedValue();
       if( ( s == "stones" && s1 == "rocks" ) ||
           ( s == "gaint" && s1 == "huge" ) ||
           ( s == "gaint" && s1 == "gigantic" ) ||
           ( s == "throw" && s1 == "toss" ) ||
           ( s == "like" && s1 == "enjoy" ) ||
           ( s == "warm" && s1 == "hot" ) )
          Tpanel.set( "You have selected the correct answer" );
        Tpanel.set( "You have selected the wrong answer" );

          wrong = 6 - right/2;
   }//end of valueChanged

  public void actionPerformed( ActionEvent evt )
    Object source = evt.getSource();

      if( source == result_Button )
        Tpanel.set( "You have " + right + " right, and you have" + wrong + " wrong" );

   }//end of actionPerforme

 }//end of list_Frame class
public class list_Test
  public static void main( String[] args ) throws NullPointerException
    JFrame f = new list_Frame();
Question by:sirtrunks
  • 2

Accepted Solution

Exceter earned 300 total points
ID: 8096552
>> private JPanel result_Panel;

You are getting the NullPointerException because you are not initializing result_Panel.

Insert this,

result_Panel = new JPanel();

before line 138 and the program will run fine.


Expert Comment

ID: 9447257
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
Post your closing recommendations!  No comment means you don't care.

Expert Comment

ID: 9451016
Points to: Exceter.

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
Simple Linear Regression
Six Sigma Control Plans

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question