Reputation: 123
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?
Upvotes: 0
Views: 1651
Reputation: 121609
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.
Upvotes: 1