在输入流上获取NULL指针异常

时间:2014-03-04 17:20:22

标签: java

不太确定为什么但是在声明输入流是什么以及它应该采取什么(在输入流中转换字符串)时,我得到一个空指针异常,get.bytes(从字符串转换为输入读取器)我读过在线并实施

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低于

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 . . .

1 个答案:

答案 0 :(得分:1)

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

col是null,这就是col.getBytes()抛出NullPointerException

的原因

顺便说一下。输出Column: null

也表示