Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: NullPointerException stopping me from deploying. Help please?

  1. #1
    Junior Member
    Join Date
    Mar 2012
    Posts
    13
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default NullPointerException stopping me from deploying. Help please?

    Hey everyone,

    I'm trying to deploy my program and I'm receiving this message:

    Exception in thread "main" java.lang.NullPointerException
    at javax.swing.ImageIcon.<init><Unknown Source>
    at MiniMain.<init><MiniMain.java:36>
    at MiniMain.main<MiniMain.java:14>


    My program extends JFrame and also has multiple frames. The code below is just all of the code from the Class holding the Main Method. I'm quite new to Java and this is my first time trying to deploy. I believe the issue is inside the Main Method but I'm not sure. Could you please explain how to fix this?

    Thanks,

    Alex

    import javax.swing.*;
    import javax.swing.ImageIcon.*;
    import java.awt.*;
    import java.awt.event.*;
     
    class MiniMain extends JFrame implements ActionListener {
     
    final static String MAINPANEL = "Menu Panel" ; final static String ADDPANEL = "Add Panel" ;
    final static String SUBPANEL = "Sub Panel" ; final static String MULPANEL = "Mul Panel" ;
    final static String DIVPANEL = "Div Panel" ;
     
    public static void main( String[] args ){
     
    new MiniMain();
     
    }
     
    //JPanels And Background Info
    JPanel mainPnl = new JPanel();
    JPanel addPnl = new JPanel();
    JPanel subPnl = new JPanel();
    JPanel mulPnl = new JPanel();
    JPanel divPnl = new JPanel();
    JPanel backPnl = new JPanel();
    JPanel scorePnl = new JPanel();
    CardLayout cardlayout = new CardLayout();
    JPanel cards = new JPanel( cardlayout );
    JDesktopPane desk = new JDesktopPane();
     
    ImageIcon backgroundImg = new ImageIcon( getClass().getResource("images/background.png") );
     
    JLabel bgImg = new JLabel( backgroundImg );
     
     
    //Main Page Content
    ImageIcon addImage = new ImageIcon( getClass().getResource("images/buttons/addbtn.png") ); ImageIcon subImage = new ImageIcon( getClass().getResource("/images/buttons/subbtn.png") );
    ImageIcon mulImage = new ImageIcon( getClass().getResource("images/buttons/mulbtn.png") ); ImageIcon divImage = new ImageIcon( getClass().getResource("/images/buttons/divbtn.png") );
     
    JLabel mainLogo = new JLabel( new ImageIcon( getClass().getResource("images/mmlogo.png") ) );
    JButton addBtn = new JButton( addImage );
    JButton subBtn = new JButton( subImage );
    JButton mulBtn = new JButton( mulImage );
    JButton divBtn = new JButton( divImage );
     
    //Game Page Content Went Here
     
    JLabel transImg1 = new JLabel( new ImageIcon( getClass().getResource("images/transparency.png") ) ); 
    JLabel transImg2 = new JLabel( new ImageIcon( getClass().getResource("images/transparency.png") ) );
    JLabel transImg3 = new JLabel( new ImageIcon( getClass().getResource("images/trans2.png") ) );
    JLabel transSmall = new JLabel( new ImageIcon( getClass().getResource("images/trans_small.png") ) );
     
    Font mainFont = new Font( "SansSerif" , Font.PLAIN , 32 ); // Font Settings
    Font scoreFont = new Font( "SansSerif" , Font.BOLD , 15 );
    Color mainColour = new Color( 255 , 255 , 255 ); // White Colour For Text
     
    String scoreStr; String streakStr; // For Scoring
     
    private static String addQuestion;
    private static String subQuestion;
    private static String mulQuestion;
    private static String divQuestion;
     
    JTextField addAnsField = new JTextField( "" , 8 ); // Answer Fields
    JTextField subAnsField = new JTextField( "" , 8 );
    JTextField mulAnsField = new JTextField( "" , 8 );
    JTextField divAnsField = new JTextField( "" , 8 );
     
    JButton helpAdd = new JButton( helpImg ); JButton helpSub = new JButton( helpImg ); // Buttons
    JButton helpMul = new JButton( helpImg ); JButton helpDiv = new JButton( helpImg );
     
    JButton submitAdd = new JButton( submitImg ); JButton submitSub = new JButton( submitImg ); JButton submitMul = new JButton( submitImg ); JButton submitDiv = new JButton( submitImg );
     
    JButton noteBtn = new JButton( noteImg );
    JButton menuAdd = new JButton( menuImg ); JButton menuSub = new JButton( menuImg );
    JButton menuMul = new JButton( menuImg ); JButton menuDiv = new JButton( menuImg );
     
    public MiniMain(){
     
    super( "Mini Math" );
    setSize( 265 , 420 );
    setDefaultCloseOperation( EXIT_ON_CLOSE );
    setResizable( false );
    setVisible( true );
    add( mainPnl ); add( addPnl ); add( subPnl ); add( mulPnl ); add( divPnl ); add( cards ); add( backPnl );
    add( scorePnl );
     
    desk.add( backPnl , new Integer( 50 ) );
    desk.add( cards , new Integer( 350 ) );
    desk.add( scorePnl , new Integer( 400 ) );
    setLayeredPane( desk );
     
     
    cards.add( mainPnl , MAINPANEL );
    cards.add( addPnl , ADDPANEL );
    cards.add( subPnl , SUBPANEL );
    cards.add( mulPnl , MULPANEL );
    cards.add( divPnl , DIVPANEL );
     
     
    backPnl.add( bgImg );
     
    cardlayout.show( cards , MAINPANEL );
    scorePnl.setVisible( false );
     
    mainPnl.add( mainLogo );
    mainPnl.add( transSmall );
    mainPnl.add( addBtn ); mainPnl.add( subBtn );
    mainPnl.add( mulBtn ); mainPnl.add( divBtn );
     
    addBtn.addActionListener( this );
    subBtn.addActionListener( this );
    mulBtn.addActionListener( this );
    divBtn.addActionListener( this );
     
    }
     
     
    public void plus(){
    cardlayout.show( cards , ADDPANEL );
     
    Numcoll.Num1() ; Numcoll.Num2();
    addQuestion = Integer.toString( Numcoll.retNum1() ) + " + " + Integer.toString( Numcoll.retNum2() );
    JLabel addQu = new JLabel( addQuestion );
    addQu.setForeground( mainColour ); addQu.setFont( mainFont );
     
    addPnl.add( whatImg );
    addPnl.add( transImg1 );
    addPnl.add( addQu );
    addPnl.add( transImg2 );
    addPnl.add( addAnsField ); addPnl.add( submitAdd );
    addPnl.add( transImg3 );
    addPnl.add( helpAdd ); addPnl.add( noteBtn ); addPnl.add( menuAdd );
     
    submitAdd.addActionListener( this );
    helpAdd.addActionListener( this );
    noteBtn.addActionListener( this );
    menuAdd.addActionListener( this );
     
    scorePnl.setVisible( true );
    scoreStr = "Score: " + Numcoll.getScore() + "    Streak: " + Numcoll.getStreak();
    JLabel scoreLbl = new JLabel( scoreStr );
    scorePnl.add( scoreLbl );
    scoreLbl.setForeground( mainColour );
    scoreLbl.setFont( scoreFont );
     
    revalidate(); repaint();
    }
     
    public void take(){
    cardlayout.show( cards , SUBPANEL );
     
    Numcoll.Num1() ; Numcoll.Num2();
    subQuestion = Integer.toString( Numcoll.retNum1() ) + " - " + Integer.toString( Numcoll.retNum2() );
    JLabel subQu = new JLabel( subQuestion );
    subQu.setForeground( mainColour ) ; subQu.setFont( mainFont );
     
    subPnl.add( whatImg );
    subPnl.add( transImg1 );
    subPnl.add( subQu );
    subPnl.add( transImg2 );
    subPnl.add( subAnsField ); subPnl.add( submitSub );
    subPnl.add( transImg3 );
    subPnl.add( helpSub ); subPnl.add( noteBtn ); subPnl.add( menuSub );
     
    submitSub.addActionListener( this );
    helpSub.addActionListener( this );
    noteBtn.addActionListener( this );
    menuSub.addActionListener( this );
     
    scorePnl.setVisible( true );
    scoreStr = "Score: " + Numcoll.getScore() + "    Streak: " + Numcoll.getStreak();
    JLabel scoreLbl = new JLabel( scoreStr );
    scorePnl.add( scoreLbl );
    scoreLbl.setForeground( mainColour );
    scoreLbl.setFont( scoreFont );
     
    revalidate(); repaint();
    }
     
    public void times(){
    cardlayout.show( cards , MULPANEL );
     
    Numcoll.mulNum1(); Numcoll.mulNum2();
    mulQuestion = Integer.toString( Numcoll.retMul1() ) + " x " + Integer.toString( Numcoll.retMul2() ) ;
    JLabel mulQu = new JLabel( mulQuestion );
    mulQu.setForeground( mainColour ); mulQu.setFont( mainFont );
     
    mulPnl.add( whatImg );
    mulPnl.add( transImg1 );
    mulPnl.add( mulQu );
    mulPnl.add( transImg2 );
    mulPnl.add( mulAnsField ); mulPnl.add( submitMul );
    mulPnl.add( transImg3 );
    mulPnl.add( helpMul ); mulPnl.add( noteBtn ); mulPnl.add( menuMul );
     
    submitMul.addActionListener( this );
    helpMul.addActionListener( this );
    noteBtn.addActionListener( this );
    menuMul.addActionListener( this );
     
    scorePnl.setVisible( true );
    scoreStr = "Score: " + Numcoll.getScore() + "    Streak: " + Numcoll.getStreak();
    JLabel scoreLbl = new JLabel( scoreStr );
    scorePnl.add( scoreLbl );
    scoreLbl.setForeground( mainColour );
    scoreLbl.setFont( scoreFont );
     
    revalidate(); repaint();
    }
     
    public void divide(){
    cardlayout.show( cards , DIVPANEL );
     
    Numcoll.divNum1(); Numcoll.divNum2(); Numcoll.divNums();
    divQuestion = Integer.toString( Numcoll.retDiv1() ) + " / " + Integer.toString( Numcoll.retDiv2() );
    JLabel divQu = new JLabel( divQuestion );
    divQu.setForeground( mainColour ); divQu.setFont( mainFont );
     
    divPnl.add( whatImg );
    divPnl.add( transImg1 );
    divPnl.add( divQu );
    divPnl.add( transImg2 );
    divPnl.add( divAnsField ); divPnl.add( submitDiv );
    divPnl.add( transImg3 );
    divPnl.add( helpDiv ); divPnl.add( noteBtn ); divPnl.add( menuDiv );
     
    submitDiv.addActionListener( this );
    helpDiv.addActionListener( this );
    noteBtn.addActionListener( this );
    menuDiv.addActionListener( this );
     
    scorePnl.setVisible( true );
    scoreStr = "Score: " + Numcoll.getScore() + "    Streak: " + Numcoll.getStreak();
    JLabel scoreLbl = new JLabel( scoreStr );
    scorePnl.add( scoreLbl );
    scoreLbl.setForeground( mainColour );
    scoreLbl.setFont( scoreFont );
     
    revalidate(); repaint();
     
    }
     
     
    public void actionPerformed( ActionEvent event ){
     
    if( event.getSource() == addBtn ) { plus(); }
    if( event.getSource() == subBtn ) { take(); }
    if( event.getSource() == mulBtn ) { times(); }
    if( event.getSource() == divBtn ) { divide(); }
    if( event.getSource() == menuAdd ) { cardlayout.show( cards , MAINPANEL ); addPnl.remove( 2 ); scorePnl.removeAll(); Numcoll.resetStreak(); }
    if( event.getSource() == menuSub ) { cardlayout.show( cards , MAINPANEL ); subPnl.remove( 2 ); scorePnl.removeAll(); Numcoll.resetStreak(); }
    if( event.getSource() == menuMul ) { cardlayout.show( cards , MAINPANEL ); mulPnl.remove( 2 ); scorePnl.removeAll(); Numcoll.resetStreak(); }
    if( event.getSource() == menuDiv ) { cardlayout.show( cards , MAINPANEL ); divPnl.remove( 2 ); scorePnl.removeAll(); Numcoll.resetStreak(); }
    if( event.getSource() == noteBtn ) { MiniPad.pad(); return; }
    if( event.getSource() == helpAdd ) { AddHelp.helpPage(); }
    if( event.getSource() == helpSub ) { SubHelp.helpPage(); }
    if( event.getSource() == helpMul ) { MulHelp.helpPage(); }
    if( event.getSource() == helpDiv ) { DivHelp.helpPage(); }
     
    if( event.getSource() == submitAdd ){
     
    try{
    int temp = Integer.parseInt( addAnsField.getText() ) ; Numcoll.storeAddEntered( temp );
    }
    catch( NumberFormatException e ) { addAnsField.setText( "" ); return; }
     
    if( Numcoll.retAddEntered() == Numcoll.retAddAns() )
    { JOptionPane.showMessageDialog( this , "Next Question..." , "Correct Answer!" , JOptionPane.INFORMATION_MESSAGE ); Numcoll.addToScore( 1 ); Numcoll.addToStreak( 1 ); }
    else{ JOptionPane.showMessageDialog( this , "The answer was " + Numcoll.retAddAns() + "." , "Keep Trying!" , JOptionPane.WARNING_MESSAGE ); Numcoll.resetStreak(); }
    addAnsField.setText( "" );
    addRestart();
     
    }
     
    if( event.getSource() == submitSub){
     
    //similar to add
     
    }
     
    if( event.getSource() == submitMul){
    //similar to add
     
    }
     
    if( event.getSource() == submitDiv){
    //similar to add
    }
     
     
    }
     
    public void addRestart() { //stuff }
    public void subRestart() { //stuff }
    public void mulRestart() { //stuff }
    public void divRestart() { //stuff }
     
    //public void showScore(){
    //scorePnl.removeAll();
    //scoreStr = "Score: " + Numcoll.getScore();
    //
     
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: NullPointerException stopping me from deploying. Help please?

    Exception in thread "main" java.lang.NullPointerException
    at javax.swing.ImageIcon.<init><Unknown Source>
    at MiniMain.<init><MiniMain.java:36>
    Look at line 36 in the source code and see what variable has a null value. Then backtrack in the code to see why that variable does not have a valid non-null value.
    If you can't tell which variable is null, add a println statement that prints out the values of all the variables used on line 36.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    RedCloudTsunami (August 22nd, 2012)

  4. #3
    Junior Member
    Join Date
    Mar 2012
    Posts
    13
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: NullPointerException stopping me from deploying. Help please?

    You were right, I'd just made a silly error on that line. I needed to uppercase a few letters and I was sorted. Many thanks! :-)

Similar Threads

  1. Deploying db application
    By drimades in forum Java IDEs
    Replies: 1
    Last Post: March 19th, 2012, 05:11 AM
  2. 500 exception while deploying a war file in tomcat
    By nrao in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 12th, 2011, 03:42 PM
  3. Deploying an applet with jsp
    By IamKira in forum JavaServer Pages: JSP & JSTL
    Replies: 0
    Last Post: January 27th, 2010, 08:28 AM
  4. error in deploying MDB
    By s.saikia in forum JavaServer Pages: JSP & JSTL
    Replies: 0
    Last Post: October 4th, 2009, 12:06 PM
  5. Error while deploying .ear file
    By urslalitha in forum Exceptions
    Replies: 1
    Last Post: August 18th, 2009, 02:56 AM