connection = new DBConnection(ParamDB.host,ParamDB.name,ParamDB.password,ParamDB.nameDB);
connection.iniProperties();
connection.init();
ResultSet resultSet = connection.query("SELECT password,name,location,education,fl,social_activ,experience,hobby,about,age FROM student where email ="+"'"+MainActivity.User+"'");
try {
while (resultSet.next()) { //вот здесь ошибка
dataDB.add(resultSet.getString("password"));
dataDB.add(resultSet.getString("name"));
dataDB.add(resultSet.getString("location"));
dataDB.add(resultSet.getString("education"));
dataDB.add(resultSet.getString("fl"));
dataDB.add(resultSet.getString("social_activ"));
dataDB.add(resultSet.getString("experience"));
dataDB.add(resultSet.getString("hobby"));
dataDB.add(resultSet.getString("about"));
dataDB.add(resultSet.getString("age"));
}
} catch (SQLException e4) {
e4.printStackTrace();
}
E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.mydb, PID: 18210 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mydb/com.example.mydb.EditProfile}: java.lang.NullPointerException: Attempt to invoke interface method 'boolean java.sql.ResultSet.next()' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2747) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2808) at android.app.ActivityThread.-wrap12(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1541) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:165) at android.app.ActivityThread.main(ActivityThread.java:6375) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:883) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773) Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'boolean java.sql.ResultSet.next()' on a null object reference at com.example.mydb.EditProfile.onCreate(EditProfile.java:46) at android.app.Activity.performCreate(Activity.java:6845) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2700)
Как это решить помогите пожалуйста?
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit_profile);
final Button button1 = (Button)findViewById(R.id.reg);
button1.setOnClickListener(this);
name = (EditText)findViewById(R.id.name);
password = (EditText)findViewById(R.id.password);
age = (EditText)findViewById(R.id.age);
experience = (EditText)findViewById(R.id.experience);
social_activ = (EditText)findViewById(R.id.social_activ);
about = (EditText)findViewById(R.id.about);
location = (EditText)findViewById(R.id.location);
education = (EditText)findViewById(R.id.education);
hobby = (EditText)findViewById(R.id.hobby);
language = (Spinner)findViewById(R.id.language);
new Thread(new Runnable() {
@Override
public void run() {
connection = new DBConnection(ParamDB.host,ParamDB.name,ParamDB.password,ParamDB.nameDB);
connection.iniProperties();
connection.init();
try {
ResultSet resultSet = connection.query("SELECT password,name,location,education,fl,social_activ,experience,hobby,about,age FROM student where email ="+"'"+MainActivity.User+"'");
while (resultSet.next()) {
dataDB.add(resultSet.getString("password"));
dataDB.add(resultSet.getString("name"));
dataDB.add(resultSet.getString("location"));
dataDB.add(resultSet.getString("education"));
dataDB.add(resultSet.getString("fl"));
dataDB.add(resultSet.getString("social_activ"));
dataDB.add(resultSet.getString("experience"));
dataDB.add(resultSet.getString("hobby"));
dataDB.add(resultSet.getString("about"));
dataDB.add(resultSet.getString("age"));
}
} catch (SQLException e4) {
e4.printStackTrace();
}
}
}).start();
System.out.println("DataDb "+dataDB.get(1)); //Ошибка
switch (dataDB.get(4)) {
case "English":
pos_lang = 0;
break;
case "Spanish":
pos_lang = 1;
break;
case "Poland":
pos_lang = 2;
break;
case "Germany":
pos_lang =3;
break;
}
System.out.println("Data elem "+dataDB);
password.setText(dataDB.get(0));
name.setText(dataDB.get(1));
location.setText(dataDB.get(2));
education.setText(dataDB.get(3));
language.setSelection(pos_lang);
social_activ.setText(dataDB.get(5));
experience.setText(dataDB.get(6));
hobby.setText(dataDB.get(7));
about.setText(dataDB.get(8));
age.setText(dataDB.get(9));
}
Вот так я сделал но почему-то список пуст и возникает ошибка
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.mydb, PID: 26815
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mydb/com.example.mydb.EditProfile}: java.lang.IndexOutOfBoundsException: Index: 1, Size: 0
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2747)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2808)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1541)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:165)
at android.app.ActivityThread.main(ActivityThread.java:6375)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:883)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
Caused by: java.lang.IndexOutOfBoundsException: Index: 1, Size: 0
at java.util.ArrayList.get(ArrayList.java:411)
at com.example.mydb.EditProfile.onCreate(EditProfile.java:77)
at android.app.Activity.performCreate(Activity.java:6845)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2700)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2808)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1541)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:165)
at android.app.ActivityThread.main(ActivityThread.java:6375)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:883)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
Ошибка понятна resultSet == null
в момент обращения. Попробуйте так:
Connection con = ...;
try {
ResultSet rs = con.createStatement().executeQuery("Select...");
} catch (SQLException e) {
e.printStackTrace();
}
Замените свою строку
ResultSet resultSet = connection.query("SELECT password,name,location,education,fl,social_activ,experience,hobby,about,age FROM student where email ="+"'"+MainActivity.User+"'");
на
ResultSet resultSet = connection.createStatement().executeQuery("SELECT password,name,location,education,fl,social_activ,experience,hobby,about,age FROM student where email ="+"'"+MainActivity.User+"'");
Виртуальный выделенный сервер (VDS) становится отличным выбором
Я получаю String jsonObject в контроллереСтруктура файла следующая:
Проблема в том, что код который я написал отлично работает на андроид устройствах и на компьютерах, а на яблочных девайсах и виндофонах криво...