• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

null pointer access the variable can only be null at this location

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm getting an error on line 137 and all it is



The error happens only after I press a JButton and it is



There is a lot more to it but that has nothing to do with my code it's just the stuff with Eclipse anyway I want to know how do I fix this problem?
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Michelle Ruth wrote:I'm getting an error on line 137 and all it is


If that's the line that's causing the exception then the rs variable is null.

To fix it you have to ensure the rs variable is not null. Based on one line of code, nobody here is going to be able to tell you how to do that.
 
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Michelle.

Either rs is null, or getMetaData() performs an operation that causes NullPointerException. It's probably the former.

You should find out why rs is null. Did you forget to set it somewhere?
 
Michelle Ruth
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well I had to set it to null because other than that Eclipse kept giving me an error

This is the program that is wrong but when I take out the = null I keep getting an error that tells me to initialize the variable


 
Michelle Ruth
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:

You should find out why rs is null. Did you forget to set it somewhere?



Well every time I didn't set it to null I kept getting an error so I'm unsure of what to do exactly
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You've only created a PreparedStatement - you also have to execute it to actually fetch data from the DB. When you execute it you will get a ResultSet back - this is what you need to assign to the rs variable.
 
Michelle Ruth
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Joanne Neal wrote:You've only created a PreparedStatement - you also have to execute it to actually fetch data from the DB. When you execute it you will get a ResultSet back - this is what you need to assign to the rs variable.



And how do I go about that exactly if you don't mind me asking
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
JDBC Tutorial
 
Michelle Ruth
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Joanne Neal wrote:JDBC Tutorial



And this will work using PHPMyAdmin?
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Michelle Ruth wrote:And this will work using PHPMyAdmin?


PHPMyAdmin appears to be a client interface for managing MySQL databases. I don't see how it is connected to your question.
JDBC is an API for accessing databases in a db independent way. The same code will work with multiple databases and the only thing you should need to change is which driver you use.
You are already using the JDBC API for connecting to the database, creating your SQL query and retrieving the metadata. You just need to add the step of initialising the variables in your PreparedStatement and then executing the query.
The link I posted explains how to do all this.

I'm assuming that as you've written this code, you already know most of this, but some of your questions suggest that maybe you don't. That's okay, but it will make it easier to help you if we know what your level of Java knowledge is. Do you actually understand all the code you have posted ?
 
Michelle Ruth
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well I know what most of it means yes but its more the database part since my lecturer hasn't explained it that well and I'm mostly on my own doing this. Our lecturer did tell us though that we could use phpmyadmin and that's the database that I'm connecting to. So it does work with java but I can't get the query to work.
 
Stephan van Hulst
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
phpMyAdmin is not a database. It's a user interface, written in PHP, to access MySQL databases. You can also access these databases programmatically, through JDBC.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Michelle Ruth wrote:So it does work with java but I can't get the query to work.


Have a look at this PreparedStatement example.

I've just noticed that you are doing an UPDATE rather than a SELECT, so there won't be any ResultSet, so you don't need the rs variable and lines 136 - 170 of your code can be got rid of.
The only things you need to do are assign values to the four question marks on line 128 of your code and then call the executeUpdate method.
You do the first with the PreparedStatement.setXXX methods - which ones depends on the type of the column in your database.
For example, if the email field is a String in your db, you will have something like

The 3 indicates that you are setting the value for the 3rd of the four fields.
Once you have set the values for the four fields you then call the executeUpdate method


Note: my JDBC is a little rusty, so you may need to tweak things a littler, but the sample I linked to should get you started.
 
reply
    Bookmark Topic Watch Topic
  • New Topic