Avatar billede touel Nybegynder
30. marts 2005 - 10:44 Der er 15 kommentarer og
1 løsning

NullPointer Exception omkring Statement

hej
jeg får nullpointer exception i følgende kode

public static Statement stmt = null;

con = Connectdb.getCon();

stmt = con.createStatement(); // fejl her...

Jeg har ikke taget hele koden med her....
Avatar billede bromer Nybegynder
30. marts 2005 - 10:46 #1
Du får vel ikke oprettet forbindelsen til databasen.
Avatar billede arne_v Ekspert
30. marts 2005 - 10:48 #2
Er de 2 linier i samme metode ?

Catcher du nogle exceptions uden at udskrive dem og afslutte ?
Avatar billede touel Nybegynder
30. marts 2005 - 11:12 #3
ja, de 2 linier er i en metode. Jeg har prøvet med en try-catch om dem og den hopper ned i catch
Avatar billede arne_v Ekspert
30. marts 2005 - 11:14 #4
Må vi ikke se hele metoden ?
Avatar billede touel Nybegynder
30. marts 2005 - 11:19 #5
public class DbQuery
{
   
    public static Statement stmt = null;
    public static  ResultSet rs = null;
    public static int a;
    public static int b ;
    public static int c;
    Connectdb db;
    public static Connection con;
   
   
    public DbQuery () throws Exception
    {
        try
        {
          db = new Connectdb ();
          System.out.println("test-try");
        }
        catch (Exception e)
        {
            System.out.println("test-catch");
            e.printStackTrace();
        }
    }

   
   
    public static boolean insertJavaToOracle ( int a, int b, int c) throws Exception
        {
            try
            { 
                try
                {
                con = Connectdb.getCon();
              stmt = con.createStatement();
              System.out.println ("niauch");
                }
                catch (Exception e)
                {
                  System.out.println ("niauch-catch");
                    e.printStackTrace();
                }
              stmt.executeUpdate( " CREATE TABLE TIC (a int, b int, c double)" +       
              stmt.executeUpdate(" INSERT INTO TIC VALUES ( '" + a + "','" + b + "','"+ c + "'")) ;
                       
              con.commit();
            }
            catch (SQLException sq)
            {
              sq.printStackTrace();
              con.rollback();
            }
            finally
            {
              if (stmt != null)
              {
                  stmt.close();
              }
              if (rs != null)
              {
                  rs.close();
              }
            }
           
            return true;
        }
Avatar billede arne_v Ekspert
30. marts 2005 - 11:21 #6
Og hvordan ser Connectdb getCon() ud ?
Avatar billede touel Nybegynder
30. marts 2005 - 11:29 #7
public class Connectdb
{
   
    public static Connection con = null;
    public static Statement stmt = null;
 
 
   
         
    //Create connection to Oracle db.
  public  Connectdb ()throws Exception
  {
          try
        {
     
              String jdbcDriver = "oracle.jdbc.driver.OracleDriver"; //config.getValue("jdbc_driver");
              String jdbcUrl =    "jdbc:oracle:thin:@net-xtae-lap:1521:blablablabla"; //config.getValue("jdbc_url");
              String jdbcUser =  "xtae"; //config.getValue("jdbc_user");
              String jdbcPassword = "xtae"; // config.getValue("jdbc_password");


              DriverManager.registerDriver(new OracleDriver());
              con = DriverManager.getConnection(jdbcUrl,jdbcUser,jdbcPassword);
        }
          catch(Exception e)
        {
              e.printStackTrace();
        }


          //check if connection is valid...
          if(con == null)
          {
              System.err.println(" Error! Got an invalid connection handle.");
          }
    }
 
    public static Connection getCon()
    {
        return con;
    }
   
   
    public static void setCon(Connection con)
    {
        Connectdb.con = con;
    }
Avatar billede arne_v Ekspert
30. marts 2005 - 11:35 #8
øh

du henter connection med en static metode

og så bliver constructor aldrig kaldt

og så er connection null

og så giver con.createStatement naturligvis en null pointer exception
Avatar billede touel Nybegynder
30. marts 2005 - 11:42 #9
aha... kunne du forklare teoretisk hvorfor det ikke kan kalde konstruktøren ?

Kan jeg fjerne static under getCon () funktionen og så i den anden klasse, kalde den som : con = db.getCon (); ?
Avatar billede arne_v Ekspert
30. marts 2005 - 11:50 #10
static metoder kører jo på klassen ikke på en instans og constructor bruges
til at konstruere instanser med
Avatar billede arne_v Ekspert
30. marts 2005 - 11:51 #11
ConnectDb db = new ConnectDb();
Connection con = db.getCon();

hvor getCon er ikke statisk bør virke
Avatar billede touel Nybegynder
30. marts 2005 - 12:36 #12
Jeg har skrevet den op sådan nu......

public  boolean insertJavaToOracle ( int a, int b, int c) throws Exception
        {
            try
            { 
              db = new Connectdb ();
              con = db.getCon ();
              stmt = con.createStatement();
              System.out.println ("niauch");
              stmt.executeUpdate( " CREATE TABLE TIC (a int, b int, c double)" +       
              stmt.executeUpdate(" INSERT INTO TIC VALUES ( '" + a+ "','" + b+ "','"+ c+ "'")) ;
                       
              con.commit();
            }
            catch (SQLException sq)
            {
              sq.printStackTrace();
              con.rollback();
            }
            finally
            {
              if (stmt != null)
              {
                  stmt.close();
              }
              if (rs != null)
              {
                  rs.close();
              }
            }
           
            return true;
        }
og får følgende fejl....

java.sql.SQLException: Io-undtagelse: Connection refused(DESCRIPTION=(TMP=)(VSNNUM=135294976)(ERR=12505)(ERROR_STACK=(ERROR=(CODE=12505)(EMFI=4))))
    at oracle.jdbc.dbaccess.DBError.throwSqlException(DBError.java:168)
    at oracle.jdbc.dbaccess.DBError.throwSqlException(DBError.java:210)
    at oracle.jdbc.dbaccess.DBError.throwSqlException(DBError.java:323)
    at oracle.jdbc.driver.OracleConnection.<init>(OracleConnection.java:263)
    at oracle.jdbc.driver.OracleDriver.getConnectionInstance(OracleDriver.java:365)
    at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:260)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at Connectdb.<init>(Connectdb.java:43)
    at DbQuery.<init>(DbQuery.java:24)
    at showReadExcel.main(showReadExcel.java:18)
Error! Got an invalid connection handle.
Type pathname of the Excel doc:
Avatar billede touel Nybegynder
30. marts 2005 - 12:42 #13
getCon () er ikke statisk nu i den anden klasse...
Avatar billede arne_v Ekspert
30. marts 2005 - 13:27 #14
"Connection refused"

er jo til at forholde sig til.

Du skal checke security, om Oracle listener kører, om port 1521 er åben etc.
Avatar billede touel Nybegynder
30. marts 2005 - 19:55 #15
du havde ret arne... svar tak
Avatar billede arne_v Ekspert
30. marts 2005 - 19:57 #16
svar
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Kurser inden for grundlæggende programmering

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester