Home > Software design >  Call a Method of a class under other package
Call a Method of a class under other package

Time:08-27

I am try to return String 'terCd' to other package.

But, my app crash.

error message is

E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.switchdata, PID: 10870

java.lang.RuntimeException: Unable to start activity

java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference

at tercodecomponent.TerCode.getTerCode(TerCode.java:25)

at com.example.switchdata.MainActivity.onCreate(MainActivity.java:22)

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference


MainActivity.java

package com.example.switchdata;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;


import tercodecomponent.TerCode;

public class MainActivity extends AppCompatActivity {



    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TerCode t = new TerCode();
        String s = t.getTerCode("TERNAME");
        Log.i("i", s);



    }

TerCode.java

package tercodecomponent;

import android.content.Context;


import androidx.appcompat.app.AppCompatActivity;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.io.InputStream;

public class TerCode extends AppCompatActivity {

    public String getTerCode(String iptname){
        Context context;

        String json = "";

        String terCd = "";
        try {

            InputStream is = getAssets().open("jsons/terData.json");  //jsonfile in assests
            int fileSize = is.available();

            byte[] buffer = new byte[fileSize];
            is.read(buffer);
            is.close();


            json = new String(buffer, "UTF-8");


            JSONObject jsonObject = new JSONObject(json);
            JSONObject res = jsonObject.getJSONObject("res");


            JSONArray array = res.getJSONArray("NAME_LIST");

           for (int i = 0; i < array.length(); i  ) {
                JSONObject object = array.getJSONObject(i);

                String terName = object.getString("trml_Nm");
                String terCode = object.getString("trml_Cd");

                if (iptname.equals(terName)) {
                    terCd = terCode;
                    break;
                }
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return terCd;
    }
}

how can I see 'Log.i("i", s);'?

CodePudding user response:

You should not be extending AppCompatActivity unless TerCode is an Activity. Remove AppCompatActivity extension and send you context of MainActivity to getTerCode method. And then use getAssets() method.

MainActivity.java

package com.example.switchdata;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;


import tercodecomponent.TerCode;

public class MainActivity extends AppCompatActivity {



    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TerCode t = new TerCode();
        String s = t.getTerCode(getApplicationContext(), "TERNAME");
        Log.i("i", s);
}

TerCode.java

package tercodecomponent;

import android.content.Context;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.io.InputStream;

public class TerCode {

    public String getTerCode(Context context, String iptname){

        String json = "";

        String terCd = "";
        try {

            InputStream is = context.getAssets().open("jsons/terData.json");  //jsonfile in assests
            int fileSize = is.available();

            byte[] buffer = new byte[fileSize];
            is.read(buffer);
            is.close();


            json = new String(buffer, "UTF-8");


            JSONObject jsonObject = new JSONObject(json);
            JSONObject res = jsonObject.getJSONObject("res");


            JSONArray array = res.getJSONArray("NAME_LIST");

           for (int i = 0; i < array.length(); i  ) {
                JSONObject object = array.getJSONObject(i);

                String terName = object.getString("trml_Nm");
                String terCode = object.getString("trml_Cd");

                if (iptname.equals(terName)) {
                    terCd = terCode;
                    break;
                }
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return terCd;
    }
}
  • Related