Results 1 to 4 of 4

Thread: Exception Error

  1. #1
    mwilliams is offline Member
    Join Date
    May 2011
    Posts
    2
    Rep Power
    0

    Default Exception Error

    I am new to Java and have a code I am trying to compile. Upon compiling I am getting an error. Tried to research exactly what the error is and how to handled it and I am not understanding what it means. Could someone please help me fix my code so it compiles correctly or point me in the right direction. Thank you for very much.

    Exception in thread "main" java.lang.NullPointerException
    at Program13.<init>(Program13.java:27)
    at Program13.main(Program13.java:68)

    Java Code:
    import java.util.Random;
    import java.awt.*;
    import javax.swing.*;
    
    public class RandomNum extends JPanel {
    
    	private int randomNumber[];
    
    public RandomNum (){
    	Random generator = new Random();
    	randomNumber = new int[100];
    	int r = 0;
    	for(int x =0; x<100; x++)
    
    	{
    	r = generator.nextInt();
    	randomNumber [x]= r;
    	}
    
    }
    
    public int getRandomNum(int index) {
    	return randomNumber[index];
    }
    
    }
    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class Program13 extends JFrame
    {
    	private JPanel panel;
    	private JPanel statusPanel;
    	private JLabel indexLabel;
    	private JLabel valueLabel;
    	private JTextField arrayIndex;
    	private JTextField arrayValue;
    	private JButton showElement;
    	private JLabel statusLine;
    
    public Program13(){
    	super ("RandomNum");
    	panel = new JPanel ();
    	indexLabel = new JLabel("Input Array Index:");
    	arrayIndex = new JTextField(10);
    	valueLabel = new JLabel("Array Element Value:");
    	arrayValue = new JTextField(10);
    	showElement = new JButton("Show Element");
    	showElement.addActionListener(new showElement());
    	statusPanel.add(statusLine);
    	panel.add(indexLabel);
    	panel.add(arrayIndex);
    	panel.add(valueLabel);
    	panel.add(arrayValue);
    	panel.add(showElement);
    	Container contentPane=getContentPane();
    	contentPane.add(panel, BorderLayout.CENTER);
    	contentPane.add(statusPanel, BorderLayout.PAGE_END);
    }
    
    public class showElement implements ActionListener{
    	public void actionPerformed(ActionEvent e){
    		String inputString;
    		int temp;
    		inputString=statusLine.getText();
    	
    		try{
    	
    			temp = Integer.parseInt(inputString);
    			statusLine.setText("");
    		}
    	
    		catch (IndexOutOfBoundsException ex)
    			{
    				statusLine.setText("Out of bounds");
    			}
    	
    		catch (NumberFormatException ex)
    			{
    				statusLine.setText("Not a number.");
    			}
    	
    	}
    }
    
    public static void main(String[] args){
    	Program13 frame= new Program13 ();
    	frame.setSize(500,500);
    	frame.setTitle("Random Numbers");
    	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	frame.setVisible(true);
    
    }
    }

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    First think, I don't see how the RandomNum class is relevant; you never seem to use it.

    After that, would you mind highlighting which lines are causing the errors?

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default

    I don't see how the RandomNum class is relevant; you never seem to use it.
    ... or why it should extend JPanel!

    db

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Quote Originally Posted by Darryl.Burke View Post
    ... or why it should extend JPanel!

    db
    I was thinking that as well, I was gonna say that there is no point in extending JPanel if he doesn't use any of the JPanel features. I figured since he didn't even use it, I wouldn't even bother.

    TBH, the whole class seems entirely worthless for this assignment. Maybe he didn't provide something about the problem?

Similar Threads

  1. Exception Error
    By unexpert in forum New To Java
    Replies: 1
    Last Post: 02-09-2011, 05:30 PM
  2. exception error
    By kira137 in forum New To Java
    Replies: 5
    Last Post: 10-12-2009, 07:46 AM
  3. Exception error
    By Rose88 in forum New To Java
    Replies: 8
    Last Post: 07-06-2009, 10:22 PM
  4. Exception error
    By jaiminparikh in forum New To Java
    Replies: 0
    Last Post: 03-20-2009, 09:06 PM
  5. JSF error+exception
    By Peter in forum SWT / JFace
    Replies: 1
    Last Post: 07-04-2007, 06:29 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •