The code below is quite simple and the only goal is to get the json data. and i put json in the string and get the particular data then it give me null pointer exception , I've been looking for a solution to this on google and I really don't get what I'm doing wrong... please help me i am new bee on android development
this is MainActivity Class
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView tv = (TextView)findViewById(R.id.Text);
String strJson="{\n"+
" \"Template\": {\n"+
" \"id\": \"32229\",\n"+
" \"Sheet\": {\n"+
" \"id\": \"T1\",\n"+
" \"Layout\": {\n"+
" \"id\": \"L1\",\n"+
" \"name\": \"AndroidLayout1\",\n"+
" \"type\": \"RelativeLayout\",\n"+
" \"width\": \"1022\",\n"+
" \"height\": \"300\",\n"+
" \"background-color\": \"none\",\n"+
" \"border-top\": \"none\",\n"+
" \"border-right\": \"none\",\n"+
" \"border-bottom\": \"none\",\n"+
" \"border-left\": \"none\",\n"+
" \"Control\": [\n"+
" {\n"+
" \"id\": \"C1\",\n"+
" \"name\": \"TextBox2\",\n"+
" \"type\": \"TextBox\",\n"+
" \"left\": \"300\",\n"+
" \"top\": \"69\",\n"+
" \"width\": \"200\",\n"+
" \"height\": \"40\",\n"+
" \"background-color\": \"rgb(102, 255, 255)\",\n"+
" \"border-width\": \"2px\",\n"+
" \"border-color\": \"rgb(204, 0, 0)\"\n"+
" },\n"+
" {\n"+
" \"id\": \"C3\",\n"+
" \"name\": \"ButtonBox4\",\n"+
" \"type\": \"ButtonBox\",\n"+
" \"left\": \"437\",\n"+
" \"top\": \"199\",\n"+
" \"width\": \"140\",\n"+
" \"height\": \"40\",\n"+
" \"background-color\": \"none\",\n"+
" \"border-top\": \"none\",\n"+
" \"border-right\": \"none\",\n"+
" \"border-bottom\": \"none\",\n"+
" \"border-left\": \"none\"\n"+
" },\n"+
" {\n"+
" \"id\": \"C5\",\n"+
" \"name\": \"Calendar6\",\n"+
" \"type\": \"Calendar\",\n"+
" \"left\": \"656\",\n"+
" \"top\": \"169\",\n"+
" \"width\": \"120\",\n"+
" \"height\": \"40\"\n"+
" }\n"+
" ]\n"+
" }\n"+
" }\n"+
" }\n"+
"}";
String data = "";
try {
// Create the root JSONObject from the JSON string.
JSONObject jsonRootObject = new JSONObject(strJson);
//Get the instance of JSONArray that contains JSONObjects
JSONArray jsonArray = jsonRootObject.optJSONArray("Layout");
//Iterate the jsonArray and print the info of JSONObjects
for(int i=0; i < jsonArray.length(); i++){
JSONObject jsonObject = jsonArray.getJSONObject(i);
int id = Integer.parseInt(jsonObject.optString("id").toString());
String name = jsonObject.optString("name").toString();
float type = Float.parseFloat(jsonObject.optString("type").toString());
data += "Node"+i+" : \n id= "+ id +" \n Name= "+ name +" \n Type= "+ type +" \n ";
}
tv.setText(data);
}catch (JSONException e) {e.printStackTrace();}
}
}
this is manifest file
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.dynamicparse"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.dynamicparse.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
this is logcat error
11-15 08:54:11.412: E/AndroidRuntime(1478): FATAL EXCEPTION: main
11-15 08:54:11.412: E/AndroidRuntime(1478): Process: com.example.dynamicparse, PID: 1478
11-15 08:54:11.412: E/AndroidRuntime(1478): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.dynamicparse/com.example.dynamicparse.MainActivity}: java.lang.NullPointerException
11-15 08:54:11.412: E/AndroidRuntime(1478): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
11-15 08:54:11.412: E/AndroidRuntime(1478): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
11-15 08:54:11.412: E/AndroidRuntime(1478): at android.app.ActivityThread.access$800(ActivityThread.java:135)
11-15 08:54:11.412: E/AndroidRuntime(1478): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
11-15 08:54:11.412: E/AndroidRuntime(1478): at android.os.Handler.dispatchMessage(Handler.java:102)
11-15 08:54:11.412: E/AndroidRuntime(1478): at android.os.Looper.loop(Looper.java:136)
11-15 08:54:11.412: E/AndroidRuntime(1478): at android.app.ActivityThread.main(ActivityThread.java:5017)
11-15 08:54:11.412: E/AndroidRuntime(1478): at java.lang.reflect.Method.invokeNative(Native Method)
11-15 08:54:11.412: E/AndroidRuntime(1478): at java.lang.reflect.Method.invoke(Method.java:515)
11-15 08:54:11.412: E/AndroidRuntime(1478): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
11-15 08:54:11.412: E/AndroidRuntime(1478): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
11-15 08:54:11.412: E/AndroidRuntime(1478): at dalvik.system.NativeStart.main(Native Method)
11-15 08:54:11.412: E/AndroidRuntime(1478): Caused by: java.lang.NullPointerException
11-15 08:54:11.412: E/AndroidRuntime(1478): at com.example.dynamicparse.MainActivity.onCreate(MainActivity.java:87)
11-15 08:54:11.412: E/AndroidRuntime(1478): at android.app.Activity.performCreate(Activity.java:5231)
11-15 08:54:11.412: E/AndroidRuntime(1478): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
11-15 08:54:11.412: E/AndroidRuntime(1478): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
11-15 08:54:11.412: E/AndroidRuntime(1478): ... 11 more