method always visible by other class

Discussion in 'Java' started by Mariano, Mar 18, 2007.

  1. Mariano

    Mariano Guest

    Then I have a class called MyDBConn.java, inside this class there's a
    method called GetResultFromPazienti():

    MyDBConn.java:
    ==================================

    package cc;

    import java.sql.*;

    public class MyDBConn {
    private Connection myConnection;
    private java.sql.Statement stmt;

    // ...

    public ResultSet getResultFromPazienti2(String query) {
    ResultSet rs=null;
    try{
    rs=stmt.executeQuery(query);
    }
    catch(Exception e){
    alerts.showErr(e.getMessage());
    }
    return rs;
    }

    // ...

    ==================================

    After this, I have a class called Paziente.java, from this class I
    would call method: getResultFromPazienti2(), class is something like
    this:

    File: Paziente.java
    ==================================
    package cc;
    import java.sql.*;

    public class Paziente extends javax.swing.JFrame {
    private MyDBConn mdbc;
    private java.sql.Statement stmt;

    // ...
    // ...

    private void formWindowOpened(java.awt.event.WindowEvent evt)
    {
    ResultSet rs=mdbc.getResultFromPazienti2("select ... from ...
    where ...");

    try {
    rs.next();
    // ...
    txtNome.setText(rs.getString("NOME"));
    // ...
    } catch (SQLException ex) {
    ex.printStackTrace();
    }
    }
    }


    ==================================

    At first moment seem that are not problems, infact there's no problem
    in compile time, but when I execute project and method
    formWindowOpened() is started an "Exception in thread "AWT-
    EventQueue-0" java.lang.NullPointerException", where are the
    problems???
    Thank you to all....
    Mariano, Mar 18, 2007
    #1
    1. Advertising

  2. Mariano wrote:
    > Then I have a class called MyDBConn.java, inside this class there's a
    > method called GetResultFromPazienti():


    The method below is called GetResultFromPazienti2().

    >
    > MyDBConn.java:
    > ==================================
    >
    > package cc;
    >
    > import java.sql.*;
    >
    > public class MyDBConn {
    > private Connection myConnection;
    > private java.sql.Statement stmt;
    >
    > // ...
    >


    the method below is not the same as you specify above.
    You never instantiate stmt so maybe that is what is producing the null
    pointer.

    > public ResultSet getResultFromPazienti2(String query) {
    > ResultSet rs=null;
    > try{
    > rs=stmt.executeQuery(query);
    > }
    > catch(Exception e){
    > alerts.showErr(e.getMessage());
    > }
    > return rs;
    > }
    >
    > // ...
    >
    > ==================================
    >
    > After this, I have a class called Paziente.java, from this class I
    > would call method: getResultFromPazienti2(), class is something like
    > this:
    >
    > File: Paziente.java
    > ==================================
    > package cc;
    > import java.sql.*;
    >
    > public class Paziente extends javax.swing.JFrame {
    > private MyDBConn mdbc;
    > private java.sql.Statement stmt;


    Is stmt ever used and initialized?
    Is mdbc ever initialized? You leave out code that prevents full analysis
    so we can't help you.

    >
    > // ...
    > // ...
    >
    > private void formWindowOpened(java.awt.event.WindowEvent evt)
    > {
    > ResultSet rs=mdbc.getResultFromPazienti2("select ... from ...
    > where ...");
    >
    > try {
    > rs.next();
    > // ...
    > txtNome.setText(rs.getString("NOME"));
    > // ...
    > } catch (SQLException ex) {
    > ex.printStackTrace();
    > }
    > }
    > }
    >
    >
    > ==================================
    >
    > At first moment seem that are not problems, infact there's no problem
    > in compile time, but when I execute project and method
    > formWindowOpened() is started an "Exception in thread "AWT-
    > EventQueue-0" java.lang.NullPointerException", where are the
    > problems???
    > Thank you to all....
    >


    The exception should tell you what line the null pointer is on. Does it?
    Brandon McCombs, Mar 19, 2007
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. spamfurnace
    Replies:
    1
    Views:
    2,624
    Scott M.
    May 24, 2004
  2. tshad

    button visible/not visible

    tshad, Oct 26, 2004, in forum: ASP .Net
    Replies:
    6
    Views:
    4,425
    Brian Hoops
    Oct 28, 2004
  3. tshad

    Sections visible and not visible

    tshad, Jan 31, 2005, in forum: ASP .Net
    Replies:
    4
    Views:
    2,045
    tshad
    Jan 31, 2005
  4. Patrick Olurotimi Ige
    Replies:
    7
    Views:
    5,938
    Patrick Olurotimi Ige
    Jun 15, 2005
  5. Pawel_Iks
    Replies:
    3
    Views:
    522
    terminator
    Jul 31, 2007
Loading...

Share This Page