Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 01-10-2010, 01:12 PM
Member
 
Join Date: Jan 2010
Posts: 2
Rep Power: 0
Borysator is on a distinguished road
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.


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);
    }

    ;
}
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();
  }
  
}
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 01:32 PM.
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 01-10-2010, 01:31 PM
Senior Member
 
Join Date: Sep 2008
Location: Voorschoten, the Netherlands
Posts: 1,237
Rep Power: 3
JosAH is on a distinguished road
Default
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
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 01-10-2010, 01:49 PM
Member
 
Join Date: Jan 2010
Posts: 2
Rep Power: 0
Borysator is on a distinguished road
Default [SOLVED] Exception in thread "main" java.lang.NullPointerException
Many thanks, I'll know this in the future.
[SOLVED]
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Exception in thread "main" java.lang.NullPointerException syarizma Advanced Java 6 08-06-2009 12:50 PM
Exception in thread "main" java.lang.NullPointerException at LinkedList.main(Link kavitha_0821 New To Java 6 07-16-2009 04:30 PM
Exception in thread "main" java.lang.NullPointerException at LinkedList.main(Link kavitha_0821 New To Java 1 07-16-2009 11:35 AM
Exception in thread "main" java.lang.NullPointerException vasavi.singh New To Java 0 02-24-2009 02:19 PM
ArrayList: Exception in thread "main" java.lang.NullPointerException susan New To Java 1 07-16-2007 07:32 AM


All times are GMT +2. The time now is 12:47 AM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org