1. Download our Official Android App: Forums for Android!

Integer.parseInt(str) crashing my app

Discussion in 'Android Lounge' started by Gregary Boyles, Oct 5, 2017.

  1. Gregary Boyles

    Thread Starter
    Rank:
    None
    Points:
    15
    Posts:
    30
    Joined:
    Oct 1, 2017

    Oct 1, 2017
    30
    0
    15
    There is clearly something I am not understanding about Integer.parseInt(String).
    Calling this function in public void set(String strIPAddress) is causing my app to crash.
    I don't get it! What am I doing wrong?

    Code (Text):
    1.  
    2. package com.example.greg.irrigationcontroller;
    3.  
    4.  
    5. /**
    6. * Created by Greg on 5/10/2017.
    7. */
    8.  
    9.  
    10. public class CIPAddress
    11. {
    12.     public CIPAddress()
    13.     {
    14.         m_arrayIP = new byte[4];
    15.         m_strDebug = new String("");
    16.     }
    17.     public CIPAddress(CharSequence strIPAddr)
    18.     {
    19.         m_arrayIP = new byte[4];
    20.         m_strDebug = new String("");
    21.         set(strIPAddr);
    22.     }
    23.     public CIPAddress(String strIPAddr)
    24.     {
    25.         m_arrayIP = new byte[4];
    26.         m_strDebug = new String("");
    27.         set(strIPAddr);
    28.     }
    29.     public CIPAddress(final int n1, final int n2, final int n3, final int n4)
    30.     {
    31.         m_arrayIP = new byte[4];
    32.         m_strDebug = new String("");
    33.         set(n1, n2, n3, n4);
    34.     }
    35.     public void set(CharSequence strIPAddress)
    36.     {
    37.         final StringBuilder sb = new StringBuilder(strIPAddress.length());
    38.         sb.append(strIPAddress);
    39.         set(strIPAddress.toString());
    40.     }
    41.     public void set(byte[] arrayIP)
    42.     {
    43.         set(arrayIP[0], arrayIP[1], arrayIP[2], arrayIP[3]);
    44.     }
    45.     public void set(String strIPAddress)
    46.     {
    47.         if (strIPAddress.length() > 0)
    48.         {
    49.             String str = new String("");
    50.             int nPos1 = 0, nPos2 = 0;
    51.  
    52.             for (int nI = 0; nI < 4; nI++)
    53.             {
    54.                 nPos2 = strIPAddress.indexOf(".", nPos1);
    55.                 if (nPos2 == -1)
    56.                     nPos2 = strIPAddress.length();
    57.  
    58.                 str = strIPAddress.substring(nPos1, nPos2);
    59.                 m_arrayIP[nI] = (byte)Integer.parseInt(str);
    60.  
    61.                 if (nI == 1)
    62.                 break;
    63.  
    64.                 nPos1 = nPos2;
    65.             }
    66.         }
    67.     }
    68.     public void set(final int n1, final int n2, final int n3, final int n4)
    69.     {
    70.         m_arrayIP[0] =(byte)n1;
    71.         m_arrayIP[1] = (byte)n2;
    72.         m_arrayIP[2] = (byte)n3;
    73.         m_arrayIP[3] = (byte)n4;
    74.     }
    75.     public String toString()
    76.     {
    77.         String strIP = new String();
    78.         strIP = String.valueOf(m_arrayIP[0] + ".");
    79.         strIP = String.valueOf(m_arrayIP[1] + ".");
    80.         strIP = String.valueOf(m_arrayIP[2] + ".");
    81.         strIP = String.valueOf(m_arrayIP[3]);
    82.         return strIP;
    83.     }
    84.     public void convertBroadcast()
    85.     {
    86.         m_arrayIP[0] &= 255;
    87.         m_arrayIP[1] &= 255;
    88.         m_arrayIP[2] &= 255;
    89.         m_arrayIP[3] = (byte)255;
    90.     }
    91.     protected byte[] m_arrayIP;
    92.     public String m_strDebug;
    93. }
    94.  
     

    Advertisement

    #1 Gregary Boyles, Oct 5, 2017
    Last edited: Oct 5, 2017

Share This Page

Loading...