1
public class MainActivity extends ActionBarActivity {
    private final String SERVER_ADDRESS = "http://10.185.14.120/sagopalgo"; //서버 주소(php파일이 저장되어있는 경로까지)

    EditText edtname;
    Button btnbuy19;
    Button btnsell19;
    Button btnbuy20;
    Button btnsell20;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        edtname = (EditText )findViewById(R.id.editText1);
        btnbuy19 = (Button )findViewById(R.id.btn1);
        btnsell19 = (Button )findViewById(R.id.btn2);
        btnbuy20 = (Button )findViewById(R.id.btn3);
        btnsell20 = (Button )findViewById(R.id.btn4);

        btnsell19.setOnClickListener(new View.OnClickListener() { //입력 버튼을 눌렀을 때1
            public void onClick(View v) {
                // TODO Auto-generated method stub

                if( edtname.getText().toString().equals("") ) { //이름이 입력 안돼있을때
                    Toast.makeText(MainActivity.this,
                            "이름을 입력하세요", Toast.LENGTH_SHORT).show();
                    return;
                }

                    runOnUiThread(new Runnable() {

                        public void run() {
                            // TODO Auto-generated method stub
                            String name = edtname.getText().toString();
                            String phone = getPhoneNumber();

                            try {
                                URL url = new URL(SERVER_ADDRESS + "/insert.php?" //테이블에 따라 php파일 다름
                                        + "name=" + URLEncoder.encode(name, "UTF-8")
                                        + "&phone=" + URLEncoder.encode(phone, "UTF-8")); //변수값을 UTF-8로 인코딩하기 위해 URLEncoder를 이용하여 인코딩함
                                url.openStream(); //서버의 DB에 입력하기 위해 웹서버의 insert.php파일에 입력된 이름과 번호를 넘김

                                String result = getXmlData("insertseller.xml", "result"); //입력 성공여부
                                if(result.equals("1")) { //result 태그값이 1일때 성공
                                    Toast.makeText(MainActivity.this,
                                            "DB insert 성공", Toast.LENGTH_SHORT).show();

                                    edtname.setText("");
                                }
                                else //result 태그값이 1이 아닐때 실패
                                    Toast.makeText(MainActivity.this,
                                            "DB insert 실패", Toast.LENGTH_SHORT).show();
                            } catch(Exception e) {
                                String err = (e.getMessage()==null)?"openStream failed":e.getMessage();
                                Log.e("err:",err);  
                            }
                        }
                    });
                }
            });
        btnsell20.setOnClickListener(new View.OnClickListener() { //입력 버튼을 눌렀을 때2
            public void onClick(View v) {
                // TODO Auto-generated method stub

                if( edtname.getText().toString().equals("") ) { //이름이 입력 안돼있을때
                    Toast.makeText(MainActivity.this,
                            "이름을 입력하세요", Toast.LENGTH_SHORT).show();
                    return;
                }

                    runOnUiThread(new Runnable() {

                        public void run() {
                            // TODO Auto-generated method stub
                            String name = edtname.getText().toString();
                            String phone = getPhoneNumber();

                            try {
                                URL url = new URL(SERVER_ADDRESS + "/insert20.php?" //테이블에 따라 php파일 다름
                                        + "name=" + URLEncoder.encode(name, "UTF-8")
                                        + "&phone=" + URLEncoder.encode(phone, "UTF-8")); //변수값을 UTF-8로 인코딩하기 위해 URLEncoder를 이용하여 인코딩함
                                url.openStream(); //서버의 DB에 입력하기 위해 웹서버의 insert.php파일에 입력된 이름과 번호를 넘김

                                String result = getXmlData("insertseller20.xml", "result"); //입력 성공여부

                                if(result.equals("1")) { //result 태그값이 1일때 성공
                                    Toast.makeText(MainActivity.this,
                                            "DB insert 성공", Toast.LENGTH_SHORT).show();

                                    edtname.setText("");
                                }
                                else //result 태그값이 1이 아닐때 실패
                                    Toast.makeText(MainActivity.this,
                                            "DB insert 실패", Toast.LENGTH_SHORT).show();
                            } catch(Exception e) {
                                Log.e("Error", e.getMessage());
                            }
                        }
                    });
                }
            });
        btnbuy19.setOnClickListener(new View.OnClickListener() { //입력 버튼을 눌렀을 때3
            public void onClick(View v) {
                // TODO Auto-generated method stub
                if( edtname.getText().toString().equals("") ) { //이름이 입력 안돼있을때
                    Toast.makeText(MainActivity.this,
                            "이름을 입력하세요", Toast.LENGTH_SHORT).show();
                    return;
                }

                    runOnUiThread(new Runnable() {

                        public void run() {
                            // TODO Auto-generated method stub
                            String name = edtname.getText().toString();
                            String phone = getPhoneNumber();     
                            String time= "19";
                            try {
                                URL url = new URL(SERVER_ADDRESS + "/delete.php?"
                                        + "name=" + URLEncoder.encode(name, "UTF-8")
                                        + "&phone=" + URLEncoder.encode(phone, "UTF-8")
                                        + "&time=" + URLEncoder.encode(time, "UTF-8")); 
                                url.openStream(); //서버의 DB에 입력하기 위해 웹서버의 insert.php파일에 입력된 이름과 번호를 넘김

                                String result = getXmlData("deleteseller.xml", "result"); //입력 성공여부
                                String seller = getXmlData("deleteseller.xml", "qrydata"); //누구의 것을 구매했는지

                                if(result.equals("1")) { //result 태그값이 1일때 성공
                                    Toast.makeText(MainActivity.this,
                                            "("+seller+")으로부터 구매성공", Toast.LENGTH_SHORT).show();
                                    edtname.setText("");
                                }
                                else //result 태그값이 1이 아닐때 실패
                                    Toast.makeText(MainActivity.this,
                                            "쿼리 실패", Toast.LENGTH_SHORT).show();
                            } catch(Exception e) {
                                Log.e("Error", e.getMessage());
                            }
                        }
                    });
                }
            });
        btnbuy20.setOnClickListener(new View.OnClickListener() { //입력 버튼을 눌렀을 때4
            public void onClick(View v) {
                // TODO Auto-generated method stub
                if( edtname.getText().toString().equals("") ) { //이름이 입력 안돼있을때
                    Toast.makeText(MainActivity.this,
                            "이름을 입력하세요", Toast.LENGTH_SHORT).show();
                    return;
                }

                    runOnUiThread(new Runnable() {

                        public void run() {
                            // TODO Auto-generated method stub
                            String name = edtname.getText().toString();
                            String phone = getPhoneNumber();     
                            String time= "20";
                            try {
                                URL url = new URL(SERVER_ADDRESS + "/delete20.php?"
                                        + "name=" + URLEncoder.encode(name, "UTF-8")
                                        + "&phone=" + URLEncoder.encode(phone, "UTF-8")
                                        + "&time=" + URLEncoder.encode(time, "UTF-8")); 
                                url.openStream(); //서버의 DB에 입력하기 위해 웹서버의 insert.php파일에 입력된 이름과 번호를 넘김

                                String result = getXmlData("deleteseller20.xml", "result"); //입력 성공여부
                                String seller = getXmlData("deleteseller20.xml", "qrydata"); //누구의 것을 구매했는지

                                if(result.equals("1")) { //result 태그값이 1일때 성공
                                    Toast.makeText(MainActivity.this,
                                            "("+seller+")으로부터 구매성공", Toast.LENGTH_SHORT).show();
                                    edtname.setText("");
                                }
                                else //result 태그값이 1이 아닐때 실패
                                    Toast.makeText(MainActivity.this,
                                            "쿼리 실패", Toast.LENGTH_SHORT).show();
                            } catch(Exception e) {
                                Log.e("Error", e.getMessage());
                            }
                        }
                    });
                }
            });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.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();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    private String getXmlData(String filename, String str) { //태그값 하나를 받아오기위한 String형 함수
        String rss = SERVER_ADDRESS + "/";
        String ret = "";

        try { //XML 파싱을 위한 과정
            XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
            factory.setNamespaceAware(true);
            XmlPullParser xpp = factory.newPullParser();
            URL server = new URL(rss + filename);
            InputStream is = server.openStream();
            xpp.setInput(is, "UTF-8");

            int eventType = xpp.getEventType();

            while(eventType != XmlPullParser.END_DOCUMENT) {
                if(eventType == XmlPullParser.START_TAG) {
                    if(xpp.getName().equals(str)) { //태그 이름이 str 인자값과 같은 경우
                        ret = xpp.nextText();
                    }
                }
                eventType = xpp.next();
            }
        } catch(Exception e) {
            Log.e("Error", e.getMessage());
        }

        return ret;
    }

    public String getPhoneNumber()  //핸드폰번호 얻기
    {
     TelephonyManager mgr = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
     return mgr.getLine1Number();
    }
}

hello

openStream get nullPointerException in runOnUiThread. i can't find solution.

11-18 16:20:02.694: E/AndroidRuntime(700): FATAL EXCEPTION: main 11-18 16:20:02.694: E/AndroidRuntime(700): java.lang.NullPointerException: println needs a message 11-18 16:20:02.694: E/AndroidRuntime(700): at android.util.Log.println_native(Native Method) 11-18 16:20:02.694: E/AndroidRuntime(700): at android.util.Log.e(Log.java:231) 11-18 16:20:02.694: E/AndroidRuntime(700): at com.example.buyandsell.MainActivity$1$1.run(MainActivity.java:77) 11-18 16:20:02.694: E/AndroidRuntime(700): at android.app.Activity.runOnUiThread(Activity.java:4591) 11-18 16:20:02.694: E/AndroidRuntime(700): at com.example.buyandsell.MainActivity$1.onClick(MainActivity.java:53) 11-18 16:20:02.694: E/AndroidRuntime(700): at android.view.View.performClick(View.java:4084) 11-18 16:20:02.694: E/AndroidRuntime(700): at android.view.View$PerformClick.run(View.java:16966) 11-18 16:20:02.694: E/AndroidRuntime(700): at android.os.Handler.handleCallback(Handler.java:615) 11-18 16:20:02.694: E/AndroidRuntime(700): at android.os.Handler.dispatchMessage(Handler.java:92) 11-18 16:20:02.694: E/AndroidRuntime(700): at android.os.Looper.loop(Looper.java:137) 11-18 16:20:02.694: E/AndroidRuntime(700): at android.app.ActivityThread.main(ActivityThread.java:4745) 11-18 16:20:02.694: E/AndroidRuntime(700): at java.lang.reflect.Method.invokeNative(Native Method) 11-18 16:20:02.694: E/AndroidRuntime(700): at java.lang.reflect.Method.invoke(Method.java:511) 11-18 16:20:02.694: E/AndroidRuntime(700): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 11-18 16:20:02.694: E/AndroidRuntime(700): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 11-18 16:20:02.694: E/AndroidRuntime(700): at dalvik.system.NativeStart.main(Native Method)

junghwi
  • 11
  • 1
  • you have a problem in one try catch, as your log said , `println needs a message`, see `MainActivity.java:77` to get your error – Shayan Pourvatan Nov 18 '15 at 16:51
  • Possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – Marcelo Nov 18 '15 at 16:58
  • Please add a comment to your source code to indicate where the error occurs - according to log it should be at line 77. We don't know which line that is unfortunately since this isn't the complete file. – Jiri Tousek Nov 18 '15 at 17:29

1 Answers1

0

You need to make sure the Log.e(tag, message) call always gets a non-null message argument in addition to a correct tag argument.

You handle it correctly here in btnsell19.setOnClickListener:

String err = (e.getMessage()==null)?"openStream failed":e.getMessage();
Log.e("err:",err);

but the logging in btnsell20.setOnClickListener is left unhandled for example:

Log.e("Error", e.getMessage());

There's couple more places that need this kind of handling in yuor posted code, so I suggest you go through all your code and check that the message is handled correctly.

Jiri Tousek
  • 12,211
  • 5
  • 29
  • 43