Student
Student

Reputation: 55

Getting a NULL pointer exception on a input stream

Not quite sure why but I am getting a null pointer exception when declaring what input stream is and what it should take in (converting string in an input stream), the get.bytes (converting from string to input reader) I read up online and implemented

public void processTables()throws SQLException
{
    dmd = con.getMetaData();
    ResultSet schema= dmd.getSchemas();
    ResultSet result = dmd.getTables(null,null,"%",null);   
    while(result.next())    {       
        String tablename = result.getString(3); 
        ResultSet column = dmd.getColumns(null,null,tablename,null);
        ResultSetMetaData rmd = result.getMetaData();
        int count = rmd.getColumnCount();
        while(column.next())    {
            for(int i=1; i<=count; i++) {
                String col=column.getString(i);
                System.out.println("Column: " + col);           
                String question = null;
                InputStream col2 = new ByteArrayInputStream(col.getBytes());
                BufferedReader brin;
                brin = new BufferedReader(new InputStreamReader(col2));
                try {
                    question = brin.readLine();
                    FileWriter output = new FileWriter("Columns.txt");
                    BufferedWriter out = new BufferedWriter(output);
                    out.write(question);
                    out.close();
                }  catch (IOException io)   {
                    System.out.println("hello");
                }
            }
            ResultSet fk = dmd.getImportedKeys(con.getCatalog(),null,tablename);
            while (fk.next())   {
                String fkTableName = fk.getString("FKTABLE_NAME");
                String fkColName = fk.getString("FKCOLUMN_NAME");
                String pkTableName = fk.getString("PKTABLE_NAME");
                String pkColName = fk.getString("PKCOLUMN_NAME");
                System.out.println("Name of Table: " + tablename);      
                System.out.println("Foreign Key: [" + fkTableName + "." + fkColName + "] REFERENCES [" + pkTableName + "." + pkColName +"]\n");         
            }
        }
    } 

JAVA STACK TRACE down below

C:\Users\harma_000\Documents\Computer Science Year 2\SCC201\SCC201 13-14 CW Stud
ent Pack>java -classpath .;sqlite-jdbc4-3.8.2-SNAPSHOT.jar RefInteg
Db.constructor [lsh]
Db.Open : leaving
Column: null
Exception in thread "main" java.lang.NullPointerException
    at Db.processTables(Db.java:42)
    at RefInteg.checkDatabase(RefInteg.java:14)
    at RefInteg.go(RefInteg.java:20)
    at RefInteg.main(RefInteg.java:28)

C:\Users\harma_000\Documents\Computer Science Year 2\SCC201\SCC201 13-14 CW Stud
ent Pack>pause
Press any key to continue . . .

Upvotes: 0

Views: 2565

Answers (1)

Max Fichtelmann
Max Fichtelmann

Reputation: 3504

System.out.println("Column: " + col);           
String question = null;
InputStream col2 = new ByteArrayInputStream(col.getBytes());

col is null, which is why col.getBytes() throws the NullPointerException

Btw. is is also indicated by your output Column: null

Upvotes: 1

Related Questions