[英]null pointer when using java socket to send array to android with ObjectOutputStream and ObjectInputStream
Hi I have been trying to pass an array into a list view using ObjectOutput/InputStream and everything works fine - I can print out the array on the server side with no problem - up until the point where I try to insert the array into the view, I get a nullpointerexception - I can insert the preloaded 'animals' array and no errors are thrown. 嗨,我一直在尝试使用ObjectOutput / InputStream将数组传递到列表视图中,并且一切正常-我可以在服务器端毫无问题地打印出数组-直到尝试将数组插入视图中为止,我得到一个nullpointerexception-我可以插入预加载的“动物”数组,并且不会引发任何错误。 Any help would be greatly appreciated as I have been stuck for weeks, thanks.
任何帮助将不胜感激,因为我已经被困了几个星期了,谢谢。
DatabaseConnection.java (server side) 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
}
}
}
当您关闭打印编写器时,您正在关闭套接字,然后从同一套接字读取时必须得到一个被忽略的异常。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.