1
FATAL EXCEPTION: main
                                               Process: ---, PID: 26657
                                               java.lang.NumberFormatException: Invalid int: "12"

This is the exception I'm encountering and I have no idea, why, because 12 seems to be a normal integer and I didn't have this problem before. It just started today, without me even touching the activity concerned. I even checked if the quotation marks are part of the string, but they aren't. Why doesn't this work. Here is the context:

AsyncTask<Uri, Void, Void> asyncTask = new AsyncTask<Uri, Void, Void>() {
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            loginButtonLayout.setVisibility(View.GONE);
            bar.setVisibility(View.VISIBLE);
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);
            ans = ans.replace(" ", "");
            ans = ans.replace("\"", "");
            Log.d("test2", ans);
            if (!(ans.equals("false")) || !(ans.equals(""))) {
                ans = ans.replace("\"", "");
                Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                SharedPreferences.Editor editor = (getBaseContext().getSharedPreferences("USER_DATA", Context.MODE_PRIVATE)).edit();
                editor.putBoolean("login", true);
                int fahrer = Integer.parseInt(ans);
                editor.putInt("fahrerId", fahrer);
                editor.clear().apply();
                ((Alpacar) getApplication()).setLoginState(true);
                ((Alpacar) getApplication()).setFahrerId(Integer.parseInt(ans));
                Toast.makeText(getBaseContext(), "Login successful", Toast.LENGTH_LONG).show();
                finish();
                startActivity(intent);
            } else {
                Toast.makeText(getBaseContext(), "Login failed", Toast.LENGTH_LONG).show();
            }
        }

        @Override
        protected Void doInBackground(Uri... uris) {
            URL url;
            url = null;
            try {
                url = new URL(uri.toString());
            } catch (MalformedURLException e) {
                e.printStackTrace();
            }
            if (url == null) {
                Log.e("MainActivity.java", "Error creating URL");
                return null;
            }


            // new try
            HttpURLConnection urlConnection = null;
            InputStream inputStream = null;
            try {
                urlConnection = (HttpURLConnection) url.openConnection();
                urlConnection.setReadTimeout(10000 /* milliseconds */);
                urlConnection.setConnectTimeout(15000 /* milliseconds */);
                urlConnection.setRequestMethod("GET");
                urlConnection.connect();
                if (urlConnection.getResponseCode() == 200) {
                    inputStream = urlConnection.getInputStream();
                    ans = readFromStream(inputStream);
                    Log.d("test", ans);
                } else {
                    Log.e("QueryUtils", "Error response code: " + urlConnection.getResponseCode());
                }

            } catch (ProtocolException e) {
                Log.e("QueryUtils", "Problem with the protocol", e);
            } catch (IOException e) {
                Log.e("QueryUtils", "Problem establishing the connection", e);

            } finally {
                if (urlConnection != null) {
                    urlConnection.disconnect();
                }
            }
            return null;
        }
    };

Problem occurs in onPostExecute...

  • Comments are not for extended discussion; this conversation has been [moved to chat](http://chat.stackoverflow.com/rooms/158965/discussion-on-question-by-adrian-breiding-numberformatexception-why-is-12-an-in). – Andy Nov 14 '17 at 13:44

2 Answers2

1

Your data contains the UTF8 BOM bytes ef bb bf before "12" and they are not printed visibly in the logcat. (You can examine by pasting the exception message to hexdump.) These bytes screw up the conversion to integer.

Fix the code that emits that value, or make your code remove the BOM bytes. For example, see Byte order mark screws up file reading in Java

laalto
  • 150,114
  • 66
  • 286
  • 303
0

Try using Integer.valueOf(ans.toString())

EXA Lab
  • 1
  • 2
  • 1
    According to this answer valuleOf uses parseInt internally. https://stackoverflow.com/questions/7355024/integer-valueof-vs-integer-parseint – Juan Nov 12 '17 at 15:21
  • @AdrianBreiding I have edited my answer, please try it out – EXA Lab Nov 12 '17 at 15:26