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 4 of 4

Thread: Help with a Java JDBC gui.

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Help with a Java JDBC gui.

    Hello guys, I'm having trouble with my final project using JDBC. I keep getting a null pointer exception and i can't figure out the problem. the code is quite long. any help would be appreciated



    import java.sql.*;
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
     
    public class Project implements ActionListener
    {
    	Connection CONN ;
    	Statement STATE;
    	ResultSet RESULT;
     
     
    	JFrame MainWindow;
     
    	JButton NEXT = new JButton("NEXT");
    	JButton PREV = new JButton("PREV");
    	JButton FIRST = new JButton("FIRST");
    	JButton LAST = new JButton("LAST");
    	JButton UPDATE = new JButton("UPDATE");
    	JButton DELETE = new JButton("DELETE");
    	JButton NEW = new JButton("NEW");
    	JButton SAVE = new JButton("SAVE");
     
    	JTextField TF_ID;
    	JTextField TF_NAME;
    	JTextField TF_SYSTEM;
    	JTextField TF_PUBLISHER;
    	JTextField TF_PRICE;
    	JTextField TF_CONDITION;
     
    	JLabel JL_ID;
    	JLabel JL_NAME;
    	JLabel JL_SYSTEM;
    	JLabel JL_PUBLISHER;
    	JLabel JL_PRICE;
    	JLabel JL_CONDITION;
     
     
    	public static void main(String args[])
    	{
    		[B]new Project();[/B]
    	}
     
    	public Project()
     
    	{
    		Connect();
    		[B]Select();[/B]
    		Write();
    		Display();
    	}
     
    	public void Write()
    	{
    		MainWindow = new JFrame();
    		MainWindow.setSize(700,200);
    		MainWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
    		JL_ID = new JLabel("ID:");
    		JL_NAME = new JLabel("NAME:");
    		JL_SYSTEM = new JLabel("System:");
    	    JL_PUBLISHER = new JLabel("Publisher:");
    	    JL_PRICE = new JLabel("Price:");
    	    JL_CONDITION = new JLabel("Condition:");
     
    		TF_ID = new JTextField(10);
    		TF_NAME = new JTextField(50);
    		TF_SYSTEM = new JTextField(20);
    		TF_PUBLISHER = new JTextField(20);
    		TF_PRICE = new JTextField(10);
    		TF_CONDITION = new JTextField(5);
     
    		JPanel BG = new JPanel();
    		BG.add(JL_ID);
    		BG.add(TF_ID);
    		BG.add(JL_NAME);
    		BG.add(TF_NAME);
    		BG.add(JL_SYSTEM);
    		BG.add(TF_SYSTEM);
    		BG.add(JL_PUBLISHER);
    		BG.add(TF_PUBLISHER);
    		BG.add(JL_PRICE);
    		BG.add(TF_PRICE);
    		BG.add(JL_CONDITION);
    		BG.add(TF_CONDITION);
     
    		BG.add(NEXT);
    		BG.add(PREV);
    		BG.add(FIRST);
    		BG.add(LAST);
    		BG.add(UPDATE);
    		BG.add(DELETE);
    		BG.add(NEW);
    		BG.add(SAVE);
     
    		NEXT.addActionListener(this);
    		PREV.addActionListener(this);
    		FIRST.addActionListener(this);
    		LAST.addActionListener(this);
    		UPDATE.addActionListener(this);
    		DELETE.addActionListener(this);
    		NEW.addActionListener(this);
    		SAVE.addActionListener(this);
     
    		MainWindow.add(BG);
    		MainWindow.setVisible(true);
    	}
     
    	public void Connect()
    	{
    		try
    		{
    			Class.forName("org.sqlite.JDBC");
    			CONN = DriverManager.getConnection("jdbc:sqlite:C:\\Users\\all others enter\\!\\!\\Desktop\\ProjectDb");
     
     
    		}
    		catch(Exception X0)
    		{}
    	}
     
    	public void Select()
    	{
    		try
    		{
    			[B]STATE = CONN.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);[/B]
    			String SQL = "SELECT * FROM games";
    			RESULT = STATE.executeQuery(SQL);
    		}
    		catch(Exception X) {}
    	}
     
    	public void Display()
    	{
    		try
    		{
    			RESULT.next();
    			TF_ID.setText(RESULT.getString("ID"));
    			TF_NAME.setText(RESULT.getString("Name"));
    		    TF_SYSTEM.setText(RESULT.getString("System"));
    			TF_PUBLISHER.setText(RESULT.getString("Publisher"));
    			TF_PRICE.setText(RESULT.getString("Price"));
    			TF_CONDITION.setText(RESULT.getString("Condition"));
    		}
    		catch(Exception X) {}
    	}
     
    	public void actionPerformed(ActionEvent X)
    	{
    			Object SRC = X.getSource();
     
    			if(SRC == NEXT)
    			{NEXT_ACTION();}
     
    			if(SRC == PREV)
    			{PREV_ACTION();}
     
    			if(SRC == FIRST)
    			{FIRST_ACTION();}
     
    			if(SRC == LAST)
    			{LAST_ACTION();}
     
    			if(SRC == UPDATE)
    			{UPDATE_ACTION();}
     
    			if(SRC == DELETE)
    			{DELETE_ACTION();}
     
    			if(SRC == NEW)
    			{NEW_ACTION();}
     
    			if(SRC == SAVE)
    			{SAVE_ACTION();}
     
    	}
     
    	public void NEXT_ACTION()
    	{
    		try
    		{
    			if( RESULT.next())
    			{
    				TF_ID.setText(RESULT.getString("ID"));
    				TF_NAME.setText(RESULT.getString("Name"));
    				TF_SYSTEM.setText(RESULT.getString("System"));
    				TF_PUBLISHER.setText(RESULT.getString("Publisher"));
    				TF_PRICE.setText(RESULT.getString("Price"));
    				TF_CONDITION.setText(RESULT.getString("Condition"));	
    			}
    			else
    			{
    				RESULT.previous();
    				JOptionPane.showMessageDialog(null,"Reached Last Record!");
    			}
    		}
    		catch(Exception X) { System.out.print(X); }
    	}
     
    	public void PREV_ACTION()
    	{
    		try
    		{
    			if( RESULT.previous())
    			{
    				TF_ID.setText(RESULT.getString("ID"));
    				TF_NAME.setText(RESULT.getString("Name"));
    				TF_SYSTEM.setText(RESULT.getString("System"));
    				TF_PUBLISHER.setText(RESULT.getString("Publisher"));
    				TF_PRICE.setText(RESULT.getString("Price"));
    				TF_CONDITION.setText(RESULT.getString("Condition"));	
    			}
    			else
    			{
    				RESULT.next();
    				JOptionPane.showMessageDialog(null,"Reached First Record!");
    			}
    		}
    		catch(Exception X) { System.out.print(X); }
    	}
     
    	public void FIRST_ACTION()
    	{
    		try
    		{
    			RESULT.first();
    			TF_ID.setText(RESULT.getString("ID"));
    			TF_NAME.setText(RESULT.getString("Name"));
    			TF_SYSTEM.setText(RESULT.getString("System"));
    			TF_PUBLISHER.setText(RESULT.getString("Publisher"));
    			TF_PRICE.setText(RESULT.getString("Price"));
    			TF_CONDITION.setText(RESULT.getString("Condition"));	
    			}
    		catch(Exception X) { System.out.print(X); }
    	}
     
    	public void LAST_ACTION()
    	{
    		try
    		{
    			RESULT.last();
    			TF_ID.setText(RESULT.getString("ID"));
    			TF_NAME.setText(RESULT.getString("Name"));
    			TF_SYSTEM.setText(RESULT.getString("System"));
    			TF_PUBLISHER.setText(RESULT.getString("Publisher"));
    			TF_PRICE.setText(RESULT.getString("Price"));
    			TF_CONDITION.setText(RESULT.getString("Condition"));	
    		}
    		catch(Exception X) { System.out.print(X); }
    	}
     
    	public void UPDATE_ACTION()
    {
    	try
    	{
    		RESULT.updateInt("ID", Integer.parseInt(TF_ID.getText()));
    		RESULT.updateString("Name",TF_NAME.getText());
    		RESULT.updateString("System",TF_SYSTEM.getText());
    		RESULT.updateString("Publisher",TF_PUBLISHER.getText());
    		RESULT.updateDouble("Price", Double.parseDouble(TF_PRICE.getText()));
    		RESULT.updateString("Condition",TF_CONDITION.getText());
    		RESULT.updateRow();
    		JOptionPane.showMessageDialog(null,"Updated!");
    	}
    	catch(Exception X) { System.out.print(X);}
    }
     
    public void DELETE_ACTION()
    {
    	try
    	{
    		RESULT.deleteRow();
    		RESULT.previous();
    		Display();
    	}
    	catch(Exception X) { System.out.print(X);}
    }
     
    public void NEW_ACTION()
    {
    	TF_ID.setText("");
    	TF_NAME.setText("");
    	TF_SYSTEM.setText("");
    	TF_PUBLISHER.setText("");
    	TF_PRICE.setText("");
    	TF_CONDITION.setText("");
    }
     
    public void SAVE_ACTION()
    {
    	try
    	{
    		RESULT.moveToInsertRow();
     
    		RESULT.updateInt("ID", Integer.parseInt(TF_ID.getText()));
    		RESULT.updateString("Name",TF_NAME.getText());
    		RESULT.updateString("System",TF_SYSTEM.getText());
    		RESULT.updateString("Publisher",TF_PUBLISHER.getText());
    		RESULT.updateDouble("Price", Double.parseDouble(TF_PRICE.getText()));
    		RESULT.updateString("Condition",TF_CONDITION.getText());
     
    		RESULT.insertRow();
     
    		STATE.close();
    		Select();
     
    		RESULT.last();
    		Display();
     
    		JOptionPane.showMessageDialog(null,"Updated");
    	}
    	catch(Exception X) {System.out.print(X);}
    }
     
     
    }

    the stack trace says

    java.lang.NullPointerException
    at Project.Select(Project.java:127)
    at Project.(init)(Project.java:48)
    at Project.main(Project.java:41)

    I highlighted those lines in bold.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,297
    Thanks
    180
    Thanked 824 Times in 767 Posts
    Blog Entries
    5

    Default Re: Help with a Java JDBC gui.

    Look at the line of your code that the stack trace references and figure out what is null. Beyond that, I would recommend wrapping your code in the code tags and posting the full stack trace for more specific help.

  3. #3
    Junior Member
    Join Date
    Nov 2012
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Help with a Java JDBC gui.

    Quote Originally Posted by copeg View Post
    Look at the line of your code that the stack trace references and figure out what is null. Beyond that, I would recommend wrapping your code in the code tags and posting the full stack trace for more specific help.


    project stack trace.jpg

  4. #4
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: Help with a Java JDBC gui.

    Do you still have a question?

    --- Update ---

    Note, that if you still are having problems and want to show us your exception text, you're going to have to do that, get the text from the cmd window and post it here as text, not as an unreadable image. Also, consider editing your original post and add [code] [/code] tags around your posted code as copeg has suggested as well as showing us which line in your code is throwing the NPE. If you haven't yet learned, you will need to learn the process of debugging NPE's since this is something that you will need to do often early (and later) in your coding education. We can help if you give us enough readable and understandable information to be able to help you.

  5. The Following User Says Thank You to curmudgeon For This Useful Post:

    Confiscator (December 2nd, 2012)

Similar Threads

  1. How to Connect to an Excel Spreadsheet using JDBC in Java
    By JavaPF in forum JDBC and Database Tutorials
    Replies: 14
    Last Post: August 27th, 2013, 11:57 AM
  2. android using java JDBC
    By aman_chauhan in forum Android Development
    Replies: 1
    Last Post: June 12th, 2012, 04:16 PM
  3. JDBC Problem - com.mysql.jdbc.Driver
    By icu222much in forum Java Servlet
    Replies: 2
    Last Post: November 21st, 2011, 10:54 PM
  4. How to Connect to an Excel Spreadsheet using JDBC in Java
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 7
    Last Post: June 21st, 2011, 02:12 AM
  5. JDBC method in java class
    By nrao in forum JDBC & Databases
    Replies: 1
    Last Post: November 20th, 2010, 02:37 PM