2014-03-13 45 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

你关闭套接字当您关闭打印的作家,那么你一定是一个被忽略的例外当你从同一个套接字读取时。

相关问题