title: Translating Java class to Kotlin
body: <p>I'm using the Java library Volley to do my network requests. 
  For POST I wanted to use the <code>JsonArrayRequest</code> class, but I noticed that due to the internal transformation of the JSON array to a string with <code>toString()</code>, all slashes ocurring in JSON arrays are escaped such that the outcoming string is <code>"\/"</code> instead of <code>"/"</code>.
  As my JSON arrays contain strings with slashes, I copied and pasted the original JsonArrayRequest class, renamed it and added <code>.replaceAll("\\\\","")</code> in order to remove all backslashes from the string genereated by <code>toString()</code>.
  It works as it should.</p>
  
  <p>This is the Java code:</p>
  
  <pre class="lang-java prettyprint-override"><code>public class MyJsonArrayRequest extends JsonRequest&lt;JSONArray&gt; {
      public MyJsonArrayRequest(
              String url, Listener&lt;JSONArray&gt; listener, @Nullable ErrorListener errorListener) {
          super(Method.GET, url, null, listener, errorListener);
      }
  
      public MyJsonArrayRequest(
              int method,
              String url,
              @Nullable JSONArray jsonRequest,
              Listener&lt;JSONArray&gt; listener,
              @Nullable ErrorListener errorListener) {
          super(
                  method,
                  url,
                  jsonRequest.toString().replaceAll("\\\\",""),
                  listener,
                  errorListener);
      }
  
      @Override
      protected Response&lt;JSONArray&gt; parseNetworkResponse(NetworkResponse response) {
          try {
              String jsonString =
                      new String(
                              response.data,
                              HttpHeaderParser.parseCharset(response.headers, PROTOCOL_CHARSET));
              return Response.success(
                      new JSONArray(jsonString), HttpHeaderParser.parseCacheHeaders(response));
          } catch (UnsupportedEncodingException e) {
              return Response.error(new ParseError(e));
          } catch (JSONException je) {
              return Response.error(new ParseError(je));
          }
      }
  }
  </code></pre>
  
  <p>However, as I'm using Kotlin on my project I prefer to translate the Java code into Kotlin code (I know that Java classes work well with Kotlin, it's just a preference and some exercise to understand Kotlin better).</p>
  
  <p>This is the Kotlin code:</p>
  
  <pre><code>open class MyJsonArrayRequest2: JsonRequest&lt;JSONArray&gt;{
      constructor(url: String, listener: Listener&lt;JSONArray&gt;, errorListener: ErrorListener):
          super(Method.GET, url, null, listener, errorListener)
  
      constructor(method: Int, url: String, jsonRequest: JSONArray, listener: Listener&lt;JSONArray&gt;, errorListener: ErrorListener):
          super(method, url, jsonRequest.toString().replace("\\\\",""), listener, errorListener)
  
      override fun parseNetworkResponse(response: NetworkResponse?): Response&lt;JSONArray&gt;{
          try{
              val jsonString = String(response!!.data, Charset.forName(HttpHeaderParser.parseCharset(response.headers)));
              return Response.success(JSONArray(jsonString), HttpHeaderParser.parseCacheHeaders(response));
          }catch (e: UnsupportedEncodingException){
              return Response.error(ParseError(e));
          }catch (je: JSONException){
              return Response.error(ParseError(je));
          }
      }
  } 
  </code></pre>
  
  <p>If I run my app now (it compiles without error or warning) with all requests using <code>MyJsonArrayRequest2</code> instead of <code>MyJsonArrayRequest</code>, I get a <code>kotlin.KotlinNullPointerException</code> error. </p>
  
  <p>Edit: 
  I now understand: The response is not null when using the Java class, but it is null when using the Kotlin class.
  The question that arises is what is the difference between the classes that could result in such discrepancy?</p>
  
  <p>I also checked the automatic translation from Java to Kotlin, but I didn't see many differences except that the automatic translation uses <code>replaceAll()</code> instead of <code>replace()</code> and omits some <code>!!</code> and <code>?</code>. However, with <code>replaceAll()</code> I got an "Unresolved reference" error and I read somewhere that Kotlin's <code>replace()</code> is the same as Java's <code>replaceAll()</code>.</p>