Results 1 to 3 of 3
  1. #1
    Borysator is offline Member
    Join Date
    Jan 2010
    Posts
    2
    Rep Power
    0

    Default Exception in thread "main" java.lang.NullPointerException

    I'm using netbins and when i try to use .jar file I get exeption. It seems that system cannot find images and crushes at first one it tries to find

    error: img\bg3.gif
    Exception in thread "main" java.lang.NullPointerException
    at javax.swing.ImageIcon.<init>(Unknown Source)
    at game.JBackgroundPanel.<init>(JBackgroundPanel.java :21)
    at game.Menu.<init>(Menu.java:16)
    at game.Main.main(Main.java:18)

    error: img\bg3.gif shows that system cannot get to bg3.gif despite the fact that it is there.


    Java Code:
    package game;
    
    import java.awt.Graphics;
    import java.io.IOException;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.swing.ImageIcon;
    import javax.swing.JPanel;
    
    public class JBackgroundPanel extends JPanel //wewnetrzna klasa - normalny jpanel + tlo
    {
    
        private ImageIcon backgroundImage;
    
        public JBackgroundPanel(String bg) {
            ClassLoader cl = this.getClass().getClassLoader();
    
            if (cl.getResource(bg) == null) {
                System.err.print("error:\t " + bg + "\n");
            }
            backgroundImage = new ImageIcon(cl.getResource(bg));
            setOpaque(false);
        }
    
        public void paintComponent(Graphics g) {
            if (backgroundImage != null) {
                g.drawImage(backgroundImage.getImage(), 0, 0, getWidth(), getHeight(), null);
            }
            super.paintComponent(g);
        }
    
        ;
    }
    Java Code:
    package game;
    
    class Main
    {  
    	public static Menu menu1;
    	public static NewGame newgame1;
    	public static HowTo howto1;
    	public static Options options1;
    	public static Highscores highscores1;
    
    	public static int level = 1;
    	public static int punkty = 1;
    	public static int tlo = 1;
    	
      
      public static void main(String[] args)
      {
    		menu1 = new Menu();
    		//newgame1 = new NewGame();
    		howto1 = new HowTo();
    		options1 = new Options();
    		highscores1 = new Highscores();
      }
      
    }
    Java Code:
    package game;
    
    import java.applet.Applet;
    import java.net.URL;
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.event.*;
    import java.io.*;
    
    
    public class Menu extends JFrame
    {
        ClassLoader cl = this.getClass().getClassLoader();
    	//panel z tlem:
            JBackgroundPanel tlo = new JBackgroundPanel("img\\bg3.gif");
    	//JBackgroundPanel tlo = new JBackgroundPanel("img\\bg3.gif");
    	
    	//przyciski:
    	ImageIcon graj = new ImageIcon(cl.getResource( "img\\graj.gif"));
    	ImageIcon jakgrac = new ImageIcon(cl.getResource( "img\\jakgrac.gif"));
    	//ImageIcon wyniki = new ImageIcon(cl.getResource( "img\\wyniki.gif"));
    	ImageIcon opcje = new ImageIcon(cl.getResource( "img\\opcje.gif"));
    	ImageIcon koniec = new ImageIcon(cl.getResource( "img\\koniec.gif"));
    	JButton b1 = new JButton(graj);
    	JButton b2 = new JButton(jakgrac);
    	//JButton b3 = new JButton(wyniki);
    	JButton b4 = new JButton(opcje);
    	JButton b5 = new JButton(koniec);
    	
    
      public Menu()
      {
       super("Menu Gry");
       Container cp = getContentPane();
       cp.setLayout(new BorderLayout()); //FlowLayout, GridLayout(3,2)
       tlo.setLayout(new BoxLayout(tlo, BoxLayout.Y_AXIS));  //BoxLayout(tlo, BoxLayout.Y_AXIS)
       cp.add(tlo, "Center");
       
       //dodanie i pozycjonowanie przyciskow:
       tlo.add(Box.createRigidArea(new Dimension(250, 0)));
       tlo.add(Box.createRigidArea(new Dimension(0, 75)));
       tlo.add(b1);
       tlo.add(Box.createRigidArea(new Dimension(0, 45)));
       tlo.add(b2);
       tlo.add(Box.createRigidArea(new Dimension(0, 45)));
       //tlo.add(b3);
       //tlo.add(Box.createRigidArea(new Dimension(0, 25)));
       tlo.add(b4);
       tlo.add(Box.createRigidArea(new Dimension(0, 45)));
       tlo.add(b5);
       
       
       
       //ustawia marginesy na przyciskach na 0:
       b1.setMargin(new Insets(0,0,0,0));
       b2.setMargin(new Insets(0,0,0,0));
       //b3.setMargin(new Insets(0,0,0,0));
       b4.setMargin(new Insets(0,0,0,0));
       b5.setMargin(new Insets(0,0,0,0));
       
       //ustawia czarna ramke na przyciskach:
       b1.setBorder(BorderFactory.createLineBorder(Color.BLACK));
       b2.setBorder(BorderFactory.createLineBorder(Color.BLACK));
       //b3.setBorder(BorderFactory.createLineBorder(Color.BLACK));
       b4.setBorder(BorderFactory.createLineBorder(Color.BLACK));
       b5.setBorder(BorderFactory.createLineBorder(Color.BLACK));
       
       //wczytanie zapisanych opcji z pliku:
       try
       {
    		BufferedReader br = new BufferedReader(new FileReader("opcje.txt"));
    		Main.level = Integer.parseInt(br.readLine());
    		Main.punkty = Integer.parseInt(br.readLine());
    		Main.tlo = Integer.parseInt(br.readLine());
    	}
    	catch(IOException ex){
                System.err.print("DUPA EXCEPTION !!!!");
            }
    
    
       
       
       //nasluchiwacze anionimowe:
       b1.addActionListener(new ActionListener () 	  //graj
       {
    		public void actionPerformed(ActionEvent e)
    		{
    			Main.newgame1 = new NewGame(Main.level, Main.tlo, Main.punkty);
    			Main.newgame1.setVisible(true);
    			Main.newgame1.setLocation(getLocation());
    			setVisible(false);
    		}
    	}
    	);
    	
    	b2.addActionListener(new ActionListener ()   	//jak grac
        {
    		public void actionPerformed(ActionEvent e)
    		{
    			Main.howto1.setVisible(true);
    			Main.howto1.setLocation(getLocation());
    			setVisible(false);
    		}
    	}
    	);
    	
    	/*b3.addActionListener(new ActionListener ()		//wyniki
    	{
    		public void actionPerformed(ActionEvent e)
    		{
    			Main.highscores1.setVisible(true);
    			Main.highscores1.setLocation(getLocation());
    			setVisible(false);
    		}
    	}
    	);*/
    	
    	b4.addActionListener(new ActionListener ()   	//opcje
        {
    		public void actionPerformed(ActionEvent e)
    		{
    			Main.options1.setVisible(true);
    			Main.options1.setLocation(getLocation());
    			setVisible(false);
    		}
    	}
    	);
    	
    	b5.addActionListener(new ActionListener ()   	//koniec
        {
    		public void actionPerformed(ActionEvent e)
    		{
    			System.exit(0);
    		}
    	}
    	);
       
       setSize(800,600);
       setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       setVisible(true);
       setResizable(false);
       
      }
    }
    It works if I start from netbins, just .jar file crushes and I don't know why.
    Last edited by Borysator; 01-10-2010 at 12:32 PM.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,663
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Borysator View Post
    I'm using netbins and when i try to use .jar file I get exeption. It seems that system cannot find images and crushes at first one it tries to find

    Exception in thread "main" java.lang.NullPointerException
    at javax.swing.ImageIcon.<init>(Unknown Source)
    at game.Menu.<init>(Menu.java:20)
    at game.Main.main(Main.java:18)
    Entries in a .jar are not files anymore; refer to those entries relative to the .jar file itself, i.e. the .jar is a 'root directory' itself. If you have a, say, .png or .gif file stored in the same package as the class referring to it simply refer that file "YourImage.gif" when you want it as a resource; this is a relative path; you can also refer to it as "/yourpackage/YourImage.gif" by using an absolute path (absolute to the .jar).

    kind regards,

    Jos

  3. #3
    Borysator is offline Member
    Join Date
    Jan 2010
    Posts
    2
    Rep Power
    0

    Default [SOLVED] Exception in thread "main" java.lang.NullPointerException

    Many thanks, I'll know this in the future.
    [SOLVED]

Similar Threads

  1. Replies: 6
    Last Post: 08-06-2009, 11:50 AM
  2. Replies: 6
    Last Post: 07-16-2009, 03:30 PM
  3. Replies: 1
    Last Post: 07-16-2009, 10:35 AM
  4. Exception in thread "main" java.lang.NullPointerException
    By vasavi.singh in forum New To Java
    Replies: 0
    Last Post: 02-24-2009, 01:19 PM
  5. Replies: 1
    Last Post: 07-16-2007, 06:32 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
  •