Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7513243
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T00:07:09+00:00 2026-05-30T00:07:09+00:00

I have a json file I’m trying to save when btn one is clicked

  • 0

I have a json file I’m trying to save when btn one is clicked and then another btn that will display the decoded json file in a textview

    public class MainActivity extends Activity {
/** Called when the activity is first created. */

String result = "";
InputStream is = null;
TextView one = (TextView) findViewById(R.id.showView);
HttpEntity entity = null;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);


    //pulls a file from my server then saves it
    Button btn1 = (Button) findViewById(R.id.downloadBtn);
    btn1.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {


            saveJson();

        //end of onClick    
        }
    //end of onClickListener
    });

    //should show the saved file
    Button btn2 = (Button) findViewById(R.id.showBtn);
    btn2.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            showJson();

        //end of onClick    
        }
    //end of onClickListener
    });

//end of oncreate()    
}

public void saveJson(){

    TextView one = (TextView) findViewById(R.id.showView);

    try{
            HttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost("http://10.0.2.2/textures_story_list.php");

            HttpResponse response = httpClient.execute(httpPost);
            entity = response.getEntity();
//from here what do i need to add to save the file as .json
    }catch(Exception e) {
        one.setText("error3");
    }


//end of returnJson()   
}

public void showJson(){

    //from here what should i have to show the file story_list.json

try{



        is = entity.getContent();
        BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"), 8);                      
        StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            is.close();
            result = sb.toString();

    }catch(Exception e) {
        }

    try{
        JSONArray jArray = new JSONArray(result);
        String storyNames = "";
        for(int i = 0;i<jArray.length();i++){
                storyNames += jArray.getJSONObject(i).getString("story_name") + "\n"; 
        }
        one.setText(storyNames);
    }
    catch(JSONException e) {
    }
        return;


//end of returnJson()   
}



//end of class body    
}

it crashes on startup_

runtimeExecption unable to instatiate activity ComponentInfo nullpointer

oK that problem fixed here is the codes I added to save and then show the file

    public class MainActivity extends Activity {
/** Called when the activity is first created. */

String result = "";
InputStream is = null;
HttpEntity entity = null;
HttpEntity readString = null;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);


    //pulls a file from my server then saves it
    Button btn1 = (Button) findViewById(R.id.downloadBtn);
    btn1.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {


            saveJson();

        //end of onClick    
        }
    //end of onClickListener
    });

    //should show the saved file
    Button btn2 = (Button) findViewById(R.id.showBtn);
    btn2.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            showJson();

        //end of onClick    
        }
    //end of onClickListener
    });

//end of oncreate()    
}

public void saveJson(){

    TextView one = (TextView) findViewById(R.id.showView);

    try{
            HttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost("http://10.0.2.2/textures_story_list.php");

            HttpResponse response = httpClient.execute(httpPost);
            entity = response.getEntity();
//from here what do i need to add to save the file as .json
    }catch(Exception e) {
        one.setText("error3");
    }
    try{

        HttpEntity text = entity;
        FileOutputStream fOut = openFileOutput("story.json",MODE_WORLD_READABLE);
        OutputStreamWriter osw = new OutputStreamWriter(fOut);
        osw.write(text);
        osw.flush();
        osw.close();
        }catch(IOException ioe){
        }   

//end of returnJson()   
}

public void showJson(){

    //from here what should i have to show the file story_list.json

    try{
        FileInputStream fIn = openFileInput("story.json");
        InputStreamReader isr = new InputStreamReader(fIn);
        char[] inputBuffer = new char[11];
        //len is the length of that saved string in the file

        isr.read(inputBuffer);

       // String readString = new String(inputBuffer);





        isr = readString.getContent();
        BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"), 8);                      
        StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            is.close();
            result = sb.toString();

    }catch(Exception e) {
        }

    try{
        JSONArray jArray = new JSONArray(result);
        String storyNames = "";
        for(int i = 0;i<jArray.length();i++){
                storyNames += jArray.getJSONObject(i).getString("story_name") + "\n"; 
        }
        TextView one = (TextView) findViewById(R.id.showView);

        one.setText(storyNames);
    }
    catch(JSONException e) {
    }
        return;


//end of returnJson()   
}



//end of class body    
}

Can someone help me fix the errors or tell me what I’m doing wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T00:07:11+00:00Added an answer on May 30, 2026 at 12:07 am

    Two suggestions:

    1) Move this assignment into your onCreate() method:

    one = (TextView) findViewById(R.id.showView);
    

    2) If you still have the problem, then step through your code to determine exactly where you’re trying to dereference a null pointer. I’m guessing it’s probably one of your “findViewById()” calls.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a web service that queries data from this json file, but I
I have an AS 3.0 class that loads a JSON file in using a
I have a php file somejson.php that echos a json encoded array {jsonone:first json,jsontwo:second
I have this swf (flash) file that provides the json that needs to be
I have a Json file that looks like this: [ { field:val }, ....
I have a JSON file that I would like to create an multi-dimensional array
I have a JSON file that I would like to create an array from.
Here's something I want to learn and do. I have a JSON file that
I have one JSON file like following: {entries:[{title:‘foo’,id:‘01’,created:‘2010-12-06’},{title:‘bar’,id:‘02’,created:‘2010-12-05’}]} And also I have one html
I have a json file that's like for e.g. [{fu: thejimjams, su: 232104580}, {fu:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.