2014-03-13 77 views
0

您好我一直在嘗試使用ObjectOutput/InputStream傳遞數組到列表視圖,一切工作正常 - 我可以打印出服務器端的數組沒有問題 - 直到我試圖插入數組到視圖中,我得到一個nullpointerexception - 我可以插入預加載的'動物'數組,並且不會引發錯誤。任何幫助將不勝感激,因爲我堅持了幾個星期,謝謝。空指針當使用Java套接字發送數組到Android與ObjectOutputStream和ObjectInputStream

DatabaseConnection.java(服務器端)

public class DatabaseConnection implements Serializable { 

/** 
* 
*/ 
private static final long serialVersionUID = 1L; 

/** 
* @param args the command line arguments 
*/ 

private static Connection connect; 

static String sqlQuery; 
private static PreparedStatement statement; 
private static ResultSet result; 
private static String username = "rachaelwaddington"; 
private static String password = "*****"; 

private static ServerSocket serverSocket; 
private static Socket clientSocket; 
private static InputStreamReader input; 
private static ObjectOutputStream output; 
private static BufferedReader bufferedReader; 
private static int port = 10000; 


static ArrayList<String> catArray = new ArrayList<String>(); 


public static void connect() { 
    try { 


     Class.forName("oracle.jdbc.driver.OracleDriver").newInstance(); 
     connect = DriverManager.getConnection("jdbc:oracle:thin:@tom.uopnet.plymouth.ac.uk:1521:orcl", 
       username, password); 
     System.out.println("Connected to Oracle!"); 


    } catch (Exception e) { 
     e.printStackTrace(); 
     System.out.println("Error!"); 
    } 
} 

public void disconnect() throws SQLException, ClassNotFoundException { 
    connect.close(); 
} 

public static void getCats() throws SQLException, ClassNotFoundException{ 
    sqlQuery = "SELECT * FROM cat_lookup"; 
    statement = connect.prepareStatement(sqlQuery); 
    result = statement.executeQuery(); 


    try { 
     while (result.next()) { 
      String cats = result.getObject(2).toString(); 



      catArray.add(cats); 


     } 

     //System.out.println(catArray); 

    } catch (SQLException e) { 
     e.printStackTrace(); 
    } 
} 
public static void main(String[] args) { 
    connect(); 



    try { 
     serverSocket = new ServerSocket(port); //Server socket 
     System.out.println("Listening on port"); 

     } catch (IOException e) { 
     System.out.println("Could not listen on port"); 
     } 

    while (true) { 
     try { 

     clientSocket = serverSocket.accept(); //accept the client connection 
     input = new InputStreamReader(clientSocket.getInputStream()); 
     bufferedReader = new BufferedReader(input); //get the client message 
     String message = bufferedReader.readLine(); 
     input.close(); 

     System.out.println(message); 

     if (message.equals("animal")) 
     { 
     getCats(); 
     } 


     output = new ObjectOutputStream(clientSocket.getOutputStream()); 
     output.writeObject(catArray); 

     System.out.println(catArray); 

     output.flush(); 
     output.close(); 
     clientSocket.close(); 



     } catch (IOException ex) { 
     System.out.println("Problem in message reading"); 
     } 

     catch (SQLException e) 
     { 
      System.out.println("SQLException Error!"); 
     } 
     catch (ClassNotFoundException f){ 
      System.out.println("ClassNotFoundException Error!"); 
     } 
     } 

    } 

} 

AnimalFragment.Java

/** This is a listfragment class */ 
public class AnimalFragment extends ListFragment { 

/** An array of items to display in ArrayList */ 
String animals[] = new String[]{ 
     "Snail", 
     "Fox", 
     "Rabbit", 
     "Woodpecker", 
     "Mouse"  
    }; 
String[] cats = new String[57]; 

private Socket client; 
private ObjectInputStream input; 


    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 

     new SendDataTask().execute(); 

     /** Creating array adapter to set data in listview */ 
     ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity().getBaseContext(), android.R.layout.select_dialog_item, cats); 

     /** Setting the array adapter to the listview */ 
     setListAdapter(adapter); 

     return super.onCreateView(inflater, container, savedInstanceState); 




    } 

    @Override 
    public void onListItemClick(ListView l, View v, int position, long id) { 
     // TODO Auto-generated method stub 
     //super.onListItemClick(l, v, position, id); 
     startActivity(new Intent (getActivity(), SpeciesSheetActivity.class)); 
    } 

    @Override 
    public void onStart() { 
     super.onStart(); 


     getListView(); 


    } 

    private class SendDataTask extends AsyncTask<Void,Void,Void>{ 

     @Override 
     protected void onPreExecute() { 
      // TODO Auto-generated method stub 
      super.onPreExecute(); 
     } 

     @Override 
     protected Void doInBackground(Void... arg0) { 
      try { 



       client = new Socket("192.168.1.70", 10000); //connect to server 
       PrintWriter printwriter = new PrintWriter(client.getOutputStream(),true); 
       printwriter.write("animal"); //write the message to output stream 

       printwriter.flush(); 
       printwriter.close(); 

       input = new ObjectInputStream(client.getInputStream()); 
       cats = (String[]) input.readObject(); 
       input.close(); 


       client.close(); //closing the connection 

       } catch (UnknownHostException e) { 
       e.printStackTrace(); 
       } catch (IOException e) { 
       e.printStackTrace(); 
       } catch (ClassNotFoundException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 

      return null; 
     } 

     @Override 
     protected void onPostExecute(Void result) { 
      // TODO Auto-generated method stub 
      super.onPostExecute(result); 
       // Handle/Update UI Part 
     } 


    } 

} 
+0

您可能沒有正確獲取/或將數據存儲在您的AnimalFragment中。你確認數據是否正確存儲在數組中?您可能必須手動解析'cats =(String [])input.readObject();'並存儲該行的數據。希望這有助於 – Parnit

回答

0

你關閉套接字當您關閉打印的作家,那麼你一定是一個被忽略的例外當你從同一個套接字讀取時。

相關問題