我有这个例外显示当我运行我的应用程序所以我希望帮助我:) 例外是:java.lang.NullPointerException:尝试在空对象引用上调用虚方法'int java.lang.String.length()'
当我点击登录类中的dd1按钮和按钮实现(public void getData(View v))方法时,异常显示
谢谢
登录类
public class Login extends Activity {
Button DD1;
TextView r;
EditText PhoneNumber1, Password1;
ProgressBar PB;
private RequestPackage p;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
r = (TextView) findViewById(R.id.message);
PhoneNumber1 = (EditText) findViewById(R.id.PN_login);
Password1 = (EditText) findViewById(R.id.PW_login);
DD1 = (Button) findViewById(R.id.loginButton);
PB = (ProgressBar) findViewById(R.id.pro);
PB.setVisibility(View.INVISIBLE);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void getData(View v) {
PB.setVisibility(View.VISIBLE);
RequestPackage p = new RequestPackage();
p.setMethod("GET");
p.setUri("http://9aleh.com/feed.asmx/CheckAccountWithPassword");
p.setParams("phoneNumber", PhoneNumber1.getText().toString());
p.setParams("password", Password1.getText().toString());
String content = HttpManager.getData(p);
if (AccountXMLParser.parseFeedChecklogin(content)) {
r.setText("TRUE");
}else
r.setText("FALSE");
PB.setVisibility(View.INVISIBLE);
}
}
HttpManager类
public class HttpManager {
public static String getData(RequestPackage p) {
BufferedReader reader = null;
String uri = p.getUri();
if(p.getMethod().equals("GET")){
uri+= "?"+p.getEncodeParams();
}
try {
URL url = new URL(uri);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod(p.getMethod());
StringBuilder sb = new StringBuilder();
reader = new BufferedReader(new InputStreamReader(con.getInputStream()));
String line ;
int fl=0;
while ((line = reader.readLine()) != null) {
if(fl!= 0)
sb.append(line + " \n");
fl++;
}
reader.close();
return sb.toString();
} catch (Exception e) {
e.printStackTrace();
return null;
} finally {
if ((reader != null)) {
}
try {
reader.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
Logcat:
10-02 03:19:48.485 10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1273)
10-02 03:19:48.489 10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at java.net.InetAddress.lookupHostByName(InetAddress.java:431)
10-02 03:19:48.498 10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at java.net.InetAddress.getAllByNameImpl(InetAddress.java:252)
10-02 03:19:48.498 10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at java.net.InetAddress.getAllByName(InetAddress.java:215)
10-02 03:19:48.498 10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at com.android.okhttp.internal.Network$1.resolveInetAddresses(Network.java:29)
10-02 03:19:48.498 10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:188)
10-02 03:19:48.498 10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at com.android.okhttp.internal.http.RouteSelector.nextProxy(RouteSelector.java:157)
10-02 03:19:48.498 10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:100)
10-02 03:19:48.498 10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at com.android.okhttp.internal.http.HttpEngine.createNextConnection(HttpEngine.java:357)
10-02 03:19:48.498 10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at com.android.okhttp.internal.http.HttpEngine.nextConnection(HttpEngine.java:340)
10-02 03:19:48.498 10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:330)
10-02 03:19:48.498 10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:248)
10-02 03:19:48.522 10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:433)
10-02 03:19:48.522 10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:384)
10-02 03:19:48.522 10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:231)
10-02 03:19:48.522 10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at com.example.salehabdarhman.ex.HttpManager.getData(HttpManager.java:27)
10-02 03:19:48.522 10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at com.example.salehabdarhman.ex.Login.getData(Login.java:72)
10-02 03:19:48.524 10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at java.lang.reflect.Method.invoke(Native Method)
10-02 03:19:48.524 10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at android.view.View$DeclaredOnClickListener.onClick(View.java:4447)
10-02 03:19:48.524 10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at android.view.View.performClick(View.java:5198)
10-02 03:19:48.525 10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at android.view.View$PerformClick.run(View.java:21147)
10-02 03:19:48.525 10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at android.os.Handler.handleCallback(Handler.java:739)
10-02 03:19:48.525 10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:95)
10-02 03:19:48.525 10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at android.os.Looper.loop(Looper.java:148)
10-02 03:19:48.525 10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5417)
10-02 03:19:48.525 10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at java.lang.reflect.Method.invoke(Native Method)
10-02 03:19:48.526 10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
10-02 03:19:48.526 10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
10-02 03:19:48.527 10727-10727/com.example.salehabdarhman.ex W/System.err﹕ java.lang.NullPointerException: Attempt to invoke virtual method 'void java.io.BufferedReader.close()' on a null object reference
10-02 03:19:48.527 10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at com.example.salehabdarhman.ex.HttpManager.getData(HttpManager.java:45)
10-02 03:19:48.527 10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at com.example.salehabdarhman.ex.Login.getData(Login.java:72)
10-02 03:19:48.527 10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at java.lang.reflect.Method.invoke(Native Method)
10-02 03:19:48.548 10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at android.view.View$DeclaredOnClickListener.onClick(View.java:4447)
10-02 03:19:48.548 10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at android.view.View.performClick(View.java:5198)
10-02 03:19:48.551 10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at android.view.View$PerformClick.run(View.java:21147)
10-02 03:19:48.551 10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at android.os.Handler.handleCallback(Handler.java:739)
10-02 03:19:48.552 10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:95)
10-02 03:19:48.553 10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at android.os.Looper.loop(Looper.java:148)
10-02 03:19:48.553 10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5417)
10-02 03:19:48.553 10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at java.lang.reflect.Method.invoke(Native Method)
10-02 03:19:48.553 10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
10-02 03:19:48.554 10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
10-02 03:19:48.556 10727-10727/com.example.salehabdarhman.ex W/System.err﹕ java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.length()' on a null object reference
10-02 03:19:48.556 10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at java.io.StringReader.<init>(StringReader.java:47)
10-02 03:19:48.556 10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at com.example.salehabdarhman.ex.AccountXMLParser.parseFeedChecklogin(AccountXMLParser.java:77)
10-02 03:19:48.556 10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at com.example.salehabdarhman.ex.Login.getData(Login.java:73)
10-02 03:19:48.561 10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at java.lang.reflect.Method.invoke(Native Method)
10-02 03:19:48.561 10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at android.view.View$DeclaredOnClickListener.onClick(View.java:4447)
10-02 03:19:48.561 10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at android.view.View.performClick(View.java:5198)
10-02 03:19:48.561 10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at android.view.View$PerformClick.run(View.java:21147)
10-02 03:19:48.561 10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at android.os.Handler.handleCallback(Handler.java:739)
10-02 03:19:48.562 10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:95)
10-02 03:19:48.562 10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at android.os.Looper.loop(Looper.java:148)
10-02 03:19:48.562 10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5417)
10-02 03:19:48.563 10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at java.lang.reflect.Method.invoke(Native Method)
10-02 03:19:48.563 10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
10-02 03:19:48.563 10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
答案 0 :(得分:1)
看起来像一个相对简单的错误。在你的HttpManager类的finally块中。
应该是
} finally {
if ((reader != null)) {
try {
reader.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
但是我强烈建议您使用java 7+时使用try-with-resources。
所以你的getData方法会变成
public static String getData(RequestPackage p) {
= null;
String uri = p.getUri();
if(p.getMethod().equals("GET")){
uri+= "?"+p.getEncodeParams();
}
try (BufferedReader reader = new BufferedReader(new InputStreamReader(con.getInputStream()))) {
URL url = new URL(uri);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod(p.getMethod());
StringBuilder sb = new StringBuilder();
String line ;
int fl=0;
while ((line = reader.readLine()) != null) {
if(fl!= 0)
sb.append(line + " \n");
fl++;
}
return sb.toString();
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
这样更容易,您不必担心关闭资源,因为它们实现了AutoCloseable。