arzucaki arzucaki - 5 months ago 20
Java Question

How to get jsonArray as extend button class array?

Hi i have a Hotel class which is an extended button with some values in additionally. İ am sharing it below.

import android.content.Context;
import android.widget.Button;

public class Hotel extends Button{
String id;
String name;
String company_id;

public Hotel(Context context, String id, String name, String company_id) {
super(context);
this.id = id;
this.name = name;
this.company_id = company_id;
}
}


I have an other class to call soap web service which return hotels as json string.
I am tring to get json hotel array as a hotel array and return it as an array. But when i run app it says "your application stopped". How can i get solved this problem? I think its something about Context. I am sharing the service class and the class i am calling the getHotels Method.

The ServiceManager Class Which i use soap and jsonArray

import android.content.Context;
import org.json.JSONArray;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
public class ServiceManager {
private static final String METHOD_NAME = "GetHotels";
private static final String NAMESPACE = "http://tempuri.org/";
private static final String SOAP_ACTION = "http://tempuri.org/GetHotels";
private static final String URL = "http://192.168.1.170/HotelApps_WebServices.asmx";

SoapObject soapObject;
SoapSerializationEnvelope soapSerializationEnvelope;
HttpTransportSE httpTransportSE;

public static Context mContext;
public ServiceManager(Context context){
this.mContext = context;
}

public Hotel[] GetHotels(int Company_id) {

soapObject = new SoapObject(NAMESPACE, METHOD_NAME);
soapObject.addProperty("Company_id", Company_id);

soapSerializationEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
soapSerializationEnvelope.dotNet = true;
soapSerializationEnvelope.setOutputSoapObject(soapObject);

httpTransportSE = new HttpTransportSE(URL);
httpTransportSE.debug = true;

Hotel hotels[]=new Hotel[6];
try {
httpTransportSE.call(SOAP_ACTION, soapSerializationEnvelope);
SoapPrimitive soapPrimitive=(SoapPrimitive)soapSerializationEnvelope.getResponse();
JSONArray jsonHotels = new JSONArray(soapPrimitive.toString());

for (int i=0; i<6; i++) {
hotels[i]=new Hotel( mContext, jsonHotels.getJSONObject(i).getString("Hotel_id"),jsonHotels.getJSONObject(i).getString("Hotel_Name"), jsonHotels.getJSONObject(i).getString("Company_id"));

}
return hotels;
} catch (Exception ex) {
ex.printStackTrace();
return hotels;
}
}

}


and this is where i call the getHotels method

package com.example.arzucaki.sherwoodhotels;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.GridLayout;

public class Hotels extends AppCompatActivity {

Hotel hotels[];

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hotels);
GridLayout glHotels=(GridLayout) findViewById(R.id.glHotels);

Thread getHotels=new Thread(new Runnable() {
@Override
public void run() {
ServiceManager service=new ServiceManager(getApplicationContext());
hotels=service.GetHotels(1);
}
});
getHotels.start();
for (int i=0; i<hotels.length; i++) {
glHotels.addView(hotels[i]);
}
}
}


this is the logcat exception


E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.arzucaki.sherwoodhotels, PID: 5656
java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.example.arzucaki.sherwoodhotels/com.example.arzucaki.sherwoodhotels.Hotels}:
java.lang.NullPointerException: Attempt to get length of null array
at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2434)
at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2494)
at android.app.ActivityThread.access$900(ActivityThread.java:157)
at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1356)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5525)
at java.lang.reflect.Method.invoke(Native Method)
at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)
Caused by: java.lang.NullPointerException: Attempt to get length of
null array
at com.example.arzucaki.sherwoodhotels.Hotels.onCreate(Hotels.java:25)
at android.app.Activity.performCreate(Activity.java:6272)
at
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2387)
at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2494) 
at android.app.ActivityThread.access$900(ActivityThread.java:157) 
at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1356) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5525) 
at java.lang.reflect.Method.invoke(Native Method)

Answer Source

It is issue because of your array hotels[] not initialized and you trying to access in for loop. so it is throwing nullpointer exception.

You should use AsyncTask instead of Thread, modify you code like:

   public class Hotels extends AppCompatActivity {

        Hotel hotels[];

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_hotels);
            GridLayout glHotels=(GridLayout) findViewById(R.id.glHotels);
            new HotelInfo().execute();

        }

        private class HotelInfo extends AsyncTask<Void,Void,Void>{

                @Override
                protected Void doInBackground(Void... params) {
                    ServiceManager service=new ServiceManager(getApplicationContext());
                    hotels=service.GetHotels(1);
                    return null;
                }

                @Override
                protected void onPostExecute(Void aVoid) {
                    super.onPostExecute(aVoid);
                    if(hotels != null){
                        for (int i=0; i<hotels.length; i++) {
                            glHotels.addView(hotels[i]);
                        }
                    }
                }
            }
}

Hope it help you !