JSONException: Misplaced object - java

This is my code:
JSONStringer result = new JSONStringer();
for (long i = start; i <= end; i = i + day) {
ttm.put("$gte", "" + i);
ttm.put("$lte", "" + (i + day));
//code code code
int count = statisticCollection.find(query).count();
try {
result.object().key("ttm").value(i).key("count").value(count);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try {
result.endObject();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
I then get a JSONException. I also tried creating and ending the object with a different try-catch block, as below:
JSONStringer result = new JSONStringer();
try {
result.object();
} catch (Exception e) {
e.printStackTrace();
}
for (long i = start; i <= end; i = i + day) {
ttm.put("$gte", "" + i);
ttm.put("$lte", "" + (i + day));
//code code code
long count = statisticCollection.find(query).count();
try {
result.key("ttm").value(i).key("count").value(count);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try {
result.endObject();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
and creating and ending the JSONStringer in the for loop itself, as follows:
JSONStringer result = new JSONStringer();
for (long i = start; i <= end; i = i + day) {
ttm.put("$gte", "" + i);
ttm.put("$lte", "" + (i + day));
//code code code
int count = statisticCollection.find(query).count();
try {
result.object().key("ttm").value(i).key("count").value(count).endObject();
} catch (JSONException e) {
e.printStackTrace();
}
What am I doing wrong?
Thanks.

You need to use an array:
JSONStringer result = new JSONStringer();
JSONWriter array = result.array();
for (long i = start; i <= end; i = i + day) {
ttm.put("$gte", "" + i);
ttm.put("$lte", "" + (i + day));
//code code code
int count = statisticCollection.find(query).count();
try {
array.object().key("ttm").value(i).key("count").value(count).endObject();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try {
array.endArray();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

Related

Elastic Search : cannot write a field name expecting a value

Okay what I am trying to achieve here is parsing a json string and separating its fields and assigning them to variables. Next I am creating a builder to send data to the elastic for indexing. But I am getting this error which is really bugging me. I am unable to move forward.
for (int i = 0; i < jsonarray.length(); i++) {
try {
obj = jsonarray.getJSONObject(i);
System.out.println(obj.toString());
} catch (JSONException e) {
e.printStackTrace();
}
try {
type = obj.getString("type");
} catch (JSONException e) {
e.printStackTrace();
}
System.out.println("Values for select");
try {
JSONArray jContent = (JSONArray) obj.get("values");
for (int iterate = 0; iterate < jContent.length(); iterate++) {
JSONObject inner = jContent.getJSONObject(iterate);
String inner_label = (String) inner.get("label");
String val = (String) inner.get("value");
boolean sel;
try {
sel = (boolean) inner.get("selected");
} catch (JSONException j) {
sel = false;
}
builder.startObject("Extras").field("inner_lab", inner_label).field("inner_value", val).field("Selected", sel).endObject();
}
} catch (JSONException e) {
}
try {
label = obj.getString("label");
} catch (JSONException e) {
e.printStackTrace();
}
try {
access = obj.getBoolean("access");
} catch (JSONException e) {
access = false;
}
try {
role = obj.getString("role");
} catch (JSONException e) {
role = null;
}
try {
subtype = obj.getString("subtype");
} catch (JSONException e) {
subtype = null;
}
try {
maxlength = obj.getString("maxlength");
} catch (JSONException e) {
maxlength = null;
}
try {
name = obj.getString("name");
} catch (JSONException e) {
name = null;
}
try {
description = obj.getString("description");
} catch (JSONException e) {
description = null;
}
try {
classname = obj.getString("className");
} catch (JSONException e) {
classname = null;
}
try {
placeholder = obj.getString("placeholder");
} catch (JSONException e) {
placeholder = null;
}
try {
value = obj.getString("value");
} catch (JSONException e) {
value = null;
}
try {
required = obj.getBoolean("required");
} catch (JSONException e) {
required = false;
}
try {
toggle = obj.getBoolean("toggle");
} catch (JSONException e) {
toggle = false;
}
try {
inline = obj.getBoolean("inline");
} catch (JSONException e) {
inline = false;
}
try {
enableother = obj.getBoolean("other");
} catch (JSONException e) {
enableother = false;
}
try {
multipleFiles = obj.getBoolean("multiple");
} catch (JSONException e) {
multipleFiles = false;
}
try {
style = obj.getString("style");
} catch (JSONException e) {
style = null;
}
try {
min = obj.getString("min");
} catch (JSONException e) {
min = null;
}
try {
max = obj.getString("max");
} catch (JSONException e) {
max = null;
}
try {
step = obj.getString("step");
} catch (JSONException e) {
step = null;
}
try {
rows = obj.getString("rows");
} catch (JSONException e) {
rows = null;
}
// //Indexing Data..
(HERE) builder.startObject("values").field("access", access).field("role", role).field("subtype", subtype)
.field("maxlength", maxlength).field("name", name).field("description", description)
.field("className", classname).field("label", label).field("placeholder", placeholder)
.field("value", value).field("required", required).field("style", style).field("toggle", toggle)
.field("inline", inline).field("other", enableother).field("multiple", multipleFiles)
.field("content", content).field("min", min).field("max", max).field("step", step)
.field("rows", rows).endObject();
builder.endObject();
resp = client.prepareIndex("clien", userName).setSource(builder).execute().actionGet();
}
}
At (HERE) position erro keeps showing up..Cannot write a field name expecting a value.
Also is there a better way to achieve what I am trying to do here. thanks for the help.

org.json.JSONException: JSONObject["values"] is not a JSONArray."

This my json array.
[
{
"type":"select",
"label":"Select",
"className":"form-control",
"name":"select-1497355331262",
"values":[
{
"label":"Option 1",
"value":"option-1",
"selected":true
},
{
"label":"Option 2",
"value":"option-2"
},
{
"label":"Option 3",
"value":"option-3"
}
]
}
]
My Appraoch :
JSONArray js = null;
try {
js = new JSONArray(json);
} catch (JSONException e) {
e.printStackTrace();
}
JSONObject obj = null;
for (int i = 0; i < js.length(); i++) {
try {
obj = js.getJSONObject(i);
} catch (JSONException e) {
e.printStackTrace();
}
try {
type = obj.getString("type");
} catch (JSONException e) {
type = null;
}
System.out.println("Type : " + type);
try {
JSONArray jContent = obj.getJSONArray("values");
System.out.println(jContent.toString());
for (int iterate = 0; iterate < jContent.length(); iterate++) {
JSONObject inner = jContent.getJSONObject(iterate);
String inner_label = (String) inner.get("label");
System.out.println(inner_label);
String val = (String) inner.get("value");
boolean sel;
try {
sel = (boolean) inner.get("selected");
} catch (JSONException j) {
sel = false;
}
System.out.print(inner_label);
System.out.print(" " + val);
System.out.println(" " + sel);
}
} catch (JSONException e) {
System.out.println("There is some error");
}
try {
label = obj.getString("label");
} catch (JSONException e) {
e.printStackTrace();
}
try {
access = obj.getBoolean("access");
} catch (JSONException e) {
access = false;
}
try {
role = obj.getString("role");
} catch (JSONException e) {
role = null;
}
try {
subtype = obj.getString("subtype");
} catch (JSONException e) {
subtype = null;
}
try {
maxlength = obj.getString("maxlength");
} catch (JSONException e) {
maxlength = null;
}
try {
name = obj.getString("name");
} catch (JSONException e) {
name = null;
}
try {
description = obj.getString("description");
} catch (JSONException e) {
description = null;
}
try {
classname = obj.getString("className");
} catch (JSONException e) {
classname = null;
}
try {
placeholder = obj.getString("placeholder");
} catch (JSONException e) {
placeholder = null;
}
try {
value = obj.getString("value");
} catch (JSONException e) {
value = null;
}
try {
required = obj.getBoolean("required");
} catch (JSONException e) {
required = false;
}
try {
toggle = obj.getBoolean("toggle");
} catch (JSONException e) {
toggle = false;
}
try {
inline = obj.getBoolean("inline");
} catch (JSONException e) {
inline = false;
}
try {
enableother = obj.getBoolean("other");
} catch (JSONException e) {
enableother = false;
}
try {
multipleFiles = obj.getBoolean("multiple");
} catch (JSONException e) {
multipleFiles = false;
}
try {
style = obj.getString("style");
} catch (JSONException e) {
style = null;
}
try {
min = obj.getString("min");
} catch (JSONException e) {
min = null;
}
try {
max = obj.getString("max");
} catch (JSONException e) {
max = null;
}
try {
step = obj.getString("step");
} catch (JSONException e) {
step = null;
}
try {
rows = obj.getString("rows");
} catch (JSONException e) {
rows = null;
}
I am able to get all other objects values I am unable to read the values array. I have written the code for it but its directly going to the catch block printing the error message "There is some error". Thnaks for the help.
Solved...Values was not an array it was an object....So here's what I did. It worked.
try {
String j2 = obj.getString("values");
JSONArray jContent = new JSONArray(j2);
for (int iterate = 0; iterate < jContent.length(); iterate++) {
JSONObject inner = jContent.getJSONObject(iterate);
String inner_label = (String) inner.get("label");
System.out.println(inner_label);
String val = (String) inner.get("value");
boolean sel;
try {
sel = (boolean) inner.get("selected");
} catch (JSONException j) {
sel = false;
}
System.out.print(inner_label);
System.out.print(" " + val);
System.out.println(" " + sel);
}

Using client-server to move object on screen

So I'm trying to use a client-server with a keyListener. When the messages "#left" or "#right" are sent to the server, it should update the graphical object and shift it to the left by calling the map.repaint() method. Instead, it shifts the object once and then no further responses happen. Posted below is the code. Can someone offer a solution?
// the server
#Override
protected void handleMessageFromClient(Object msg, ConnectionToClient client) {
// TODO Auto-generated method stub
if(msg instanceof String)
{
String message = msg.toString();
if(message.equals("#connect"))
{
String error = "Already connected";
Matcher match = null;
for (int i = 0; i < matches.size(); i++)
{
if(matches.get(i).contains(client))
{
match = matches.get(i);
try {
client.sendToClient(error);
} catch (IOException e) {
// TODO Auto-generated catch block
System.err.println("Could not send #connect error message");
}
break;
}
}
if(match == null)
{
Matcher matcher = new Matcher(this,client);
matches.add(matcher);
}
}
if(message.equals("#left"))
{
action("left",client);
}
if(message.equals("#right"))
{
action("right",client);
}
if(message.equals("#jump"))
{
action("jump",client);
}
if(message.startsWith("#shoot"))
{
String error = "please connect";
Matcher match = null;
for (int i = 0; i < matches.size(); i++)
{
if(matches.get(i).contains(client))
{
match = matches.get(i);
break;
}
}
if(match == null)
{
try {
client.sendToClient(error);
} catch (IOException e) {
// TODO Auto-generated catch block
System.err.println("Could not send #left error message");
}
}else
{
match.moveLeft(client);
}
}
if(message.contains("?"))
try {
client.sendToClient(msg);
} catch (IOException ex) {
Logger.getLogger(Game.class.getName()).log(Level.SEVERE, null, ex);
}
}
if (msg instanceof Integer)
try {
client.sendToClient(msg);
} catch (IOException ex) {
System.out.println("Message not sent. Turning off.");
}
private void action(String errorm, ConnectionToClient client)
{
String error = "please connect";
Matcher match = null;
for (int i = 0; i < matches.size(); i++)
{
if(matches.get(i).contains(client))
{
match = matches.get(i);
break;
}
}
if(match == null)
{
try {
client.sendToClient(error);
} catch (IOException e) {
// TODO Auto-generated catch block
System.err.println("Could not send #" + errorm + " error message");
}
}else
{
if(errorm.equals("left"))
{
match.moveLeft(client);
}else if(errorm.equals("right"))
{
match.moveRight(client);
}
else if(errorm.equals("jump"))
{
match.updateJump(client);
}
}
}
//the matcher class which controls graphics
public void moveLeft(ConnectionToClient client)
{
for (int i = 0; i < players.length; i++)
{
if(players[i].getId() == client.getId())
{
if(i == 0)
{
synchronized(this)
{
map.updateFirstPlayer(map.getx1Rec1() - 5, map.gety1Rec1(), map.getx2Rec1(), map.gety2Rec1());
map.repaint();
map.updateUI();
}
}
if(i == 1)
{
synchronized(this)
{
map.updateSecondPlayer(map.getx1Rec2() - 5, map.gety1Rec2(), map.getx2Rec2(), map.gety2Rec2());
map.repaint();
map.updateUI();
}
}
break;
}
}
try {
client.sendToClient(map);
} catch (IOException e) {
// TODO Auto-generated catch block
System.err.println("Could not update left");
}
}
public void moveRight(ConnectionToClient client)
{
for (int i = 0; i < players.length; i++)
{
if(players[i].getId() == client.getId())
{
if(i == 0)
{
synchronized(this)
{
map.updateFirstPlayer(map.getx1Rec1() + 5, map.gety1Rec1(), map.getx2Rec1(), map.gety2Rec1());
map.repaint();
map.updateUI();
}
}
if(i == 1)
{
synchronized(this)
{
map.updateSecondPlayer(map.getx1Rec2() + 5, map.gety1Rec2(), map.getx2Rec2(), map.gety2Rec2());
map.repaint();
map.updateUI();
}
}
break;
}
}
try {
client.sendToClient(map);
} catch (IOException e) {
// TODO Auto-generated catch block
System.err.println("Could not update right");
}
}
public void updateJump(ConnectionToClient client)
{
for (int i = 0; i < players.length; i++)
{
if(players[i].getId() == client.getId())
{
if(i == 0)
{
synchronized(this)
{
for (int j = 0; j < 50; j+=5)
{
map.updateFirstPlayer(map.getx1Rec1() , map.gety1Rec1() + j, map.getx2Rec1(), map.gety2Rec1());
/*try {
Thread.sleep(5);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}*/
map.repaint();
}
}
}
if(i == 1)
{
synchronized(this)
{
for (int j = 0; j < 50; j+=5)
{
map.updateSecondPlayer(map.getx1Rec2(), map.gety1Rec2() + j, map.getx2Rec2(), map.gety2Rec2());
/*try {
Thread.sleep(5);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}*/
map.repaint();
map.updateUI();
}
}
}
break;
}
}
try {
client.sendToClient(map);
} catch (IOException e) {
// TODO Auto-generated catch block
System.err.println("Could not return jump");
}
}

Which copy data more faster in Java while using FileChannel.transferFrom and while()?

The following Java code in two ways to copy files which more efficient? Want to be able to explain why, thank you.
By the way, I've tested the same file which size is about 300 megabytes. The speed of the first method as fast as the second one. But after tested more times, the result is differently.
One of my test result as below:
NO.1: Using transferFrom took 3396ms
NO.1: Using while took 3334ms
NO.2: Using transferFrom took 4239ms
NO.2: Using while took 3225ms
NO.3: Using transferFrom took 3906ms
NO.3: Using while took 4153ms
NO.4: Using transferFrom took 3100ms
NO.4: Using while took 3174ms
NO.5: Using transferFrom took 3156ms
NO.5: Using while took 3180ms
Code:
public void copyData(String inPath, String outPath1, String outPath2) {
FileChannel inChannel = null;
FileChannel outChannel = null;
FileInputStream fin = null;
FileOutputStream fout = null;
File out1File = null;
File out2File = null;
int bufferLen = 20480 * 2 * 1024;
long runTime;
ByteBuffer bb = ByteBuffer.allocateDirect(bufferLen);
for (int i = 0; i < 5; i++) {
try {
fin = new FileInputStream(new File(inPath));
out1File = new File(outPath1);
fout = new FileOutputStream(out1File);
inChannel = fin.getChannel();
outChannel = fout.getChannel();
runTime = System.currentTimeMillis();
outChannel.transferFrom(inChannel, 0, inChannel.size());
runTime = System.currentTimeMillis() - runTime;
System.out.println("NO." + (i + 1) + ": "
+ "Using transferFrom took " + runTime + "ms");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (null != fin) {
try {
fin.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (null != fout) {
try {
fout.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (null != inChannel) {
try {
inChannel.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (null != outChannel) {
try {
outChannel.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
out1File.deleteOnExit();
}
/**********************************************************/
try {
fin = new FileInputStream(new File(inPath));
out2File = new File(outPath2);
fout = new FileOutputStream(out2File);
inChannel = fin.getChannel();
outChannel = fout.getChannel();
runTime = System.currentTimeMillis();
while (true) {
int ret = inChannel.read(bb);
if (ret == -1) {
break;
}
bb.flip();
outChannel.write(bb);
bb.clear();
}
runTime = System.currentTimeMillis() - runTime;
System.out.println("NO." + (i + 1) + ": " + "Using while took "
+ runTime + "ms");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (null != fin) {
try {
fin.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (null != fout) {
try {
fout.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (null != inChannel) {
try {
inChannel.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (null != outChannel) {
try {
outChannel.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
out2File.deleteOnExit();
}
}
}

Android : My app crashes when there is a blank editText field

I have a problem with my code.
It keeps on crashing when i have a blank editText field.
This bit of code is in the settings of my app and works the data fine but when there is a blank field it crashes the program.
Here's the code for it.
Please don't be harsh because it is my 1st android app. So if anyone knows how to solves the blank edit text field problem that would be greatly appreciated! (Any other comments on how to improve the app would be a help to).
Cheers
package com.cleanyet.cyv100fp;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class sTasks extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.tasks);
/*Sets Up the Variables*/
Button done = (Button) findViewById(R.id.done1);
EditText t1 = (EditText)findViewById(R.id.tbTask1);
EditText t2 = (EditText)findViewById(R.id.tbTask2);
EditText t3 = (EditText)findViewById(R.id.tbTask3);
EditText t4 = (EditText)findViewById(R.id.tbTask4);
EditText t5 = (EditText)findViewById(R.id.tbTask5);
String FILENAME1 = "stask1";
String FILENAME2 = "stask2";
String FILENAME3 = "stask3";
String FILENAME4 = "stask4";
String FILENAME5 = "stask5";
String task1 = null;
String task2 = null;
String task3 = null;
String task4 = null;
String task5 = null;
String edit = "Edit Task";
/*Fixes the Blank Field bug*/
/*Sets up the file input*/
FileInputStream fis = null;
/*Gets the tasks set previously*/
/*Task 1 set up*/
try {
fis = openFileInput(FILENAME1);
byte[] dataArray = new byte[fis.available()];
while (fis.read(dataArray) != -1){
task1 = new String(dataArray);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally {
try {
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (task1.toString().length() < 0) {
task1.toString();
t1.setText(task1);
}
else {
t1.setText(edit);
}
/*Task 2 set up*/
try {
fis = openFileInput(FILENAME2);
byte[] dataArray = new byte[fis.available()];
while (fis.read(dataArray) != -1){
task2 = new String(dataArray);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally {
try {
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (task2.toString().length() < 0) {
task2.toString();
t2.setText(task2);
}
else {
t2.setText(edit);
}
/*Task 3 set up*/
try {
fis = openFileInput(FILENAME3);
byte[] dataArray = new byte[fis.available()];
while (fis.read(dataArray) != -1){
task3 = new String(dataArray);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally {
try {
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (task3.toString().length() < 0) {
task3.toString();
t3.setText(task3);
}
else {
t3.setText(edit);
}
/*Task 4 set up*/
try {
fis = openFileInput(FILENAME4);
byte[] dataArray = new byte[fis.available()];
while (fis.read(dataArray) != -1){
task4 = new String(dataArray);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally {
try {
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (task4.toString().length() < 0) {
task4.toString();
t4.setText(task4);
}
else {
t4.setText(edit);
}
/*Task 5 set up*/
try {
fis = openFileInput(FILENAME5);
byte[] dataArray = new byte[fis.available()];
while (fis.read(dataArray) != -1){
task5 = new String(dataArray);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally {
try {
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (task5.toString().length() < 0) {
task5.toString();
t5.setText(task5);
}
else {
t5.setText(edit);
}
/*When changes have been made and done is clicked*/
done.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
/*Sets up the Variables*/
EditText t1 = (EditText)findViewById(R.id.tbTask1);
EditText t2 = (EditText)findViewById(R.id.tbTask2);
EditText t3 = (EditText)findViewById(R.id.tbTask3);
EditText t4 = (EditText)findViewById(R.id.tbTask4);
EditText t5 = (EditText)findViewById(R.id.tbTask5);
String tasks1 = t1.getText().toString();
String tasks2 = t2.getText().toString();
String tasks3 = t3.getText().toString();
String tasks4 = t4.getText().toString();
String tasks5 = t5.getText().toString();
String FILENAME1 = "stask1";
String FILENAME2 = "stask2";
String FILENAME3 = "stask3";
String FILENAME4 = "stask4";
String FILENAME5 = "stask5";
String task1 = tasks1;
String task2 = tasks2;
String task3 = tasks3;
String task4 = tasks4;
String task5 = tasks5;
String edit = "Go to settings to make this task.";
if (t1 != null){
t1.setText(edit);
/*t2.setText(edit);
t3.setText(edit);
t4.setText(edit);
t5.setText(edit);*/
};
/*Put if statement here to catch the empty field*/
/*Makes The Changes to the Tasks*/
try {
FileOutputStream fos1 = openFileOutput(FILENAME1, Context.MODE_PRIVATE);
fos1.write(task1.getBytes());
fos1.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
FileOutputStream fos2 = openFileOutput(FILENAME2, Context.MODE_PRIVATE);
fos2.write(task2.getBytes());
fos2.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
FileOutputStream fos3 = openFileOutput(FILENAME3, Context.MODE_PRIVATE);
fos3.write(task3.getBytes());
fos3.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
FileOutputStream fos4 = openFileOutput(FILENAME4, Context.MODE_PRIVATE);
fos4.write(task4.getBytes());
fos4.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
FileOutputStream fos5 = openFileOutput(FILENAME5, Context.MODE_PRIVATE);
fos5.write(task5.getBytes());
fos5.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
startActivity(new Intent("com.checkin.cyv100fp.settings"));
}
});
}
}
if (task1.toString().length() < 0) {
task1.toString();
t1.setText(task1);
}
else {
t1.setText(edit);
}
The above makes no sense at all.
Firstly task1 IS a string so there's no need to call toString() to convert it to one.
Secondly, your conditional statement (if) is checking to see if task1 has a length less than zero....think about it.
Thirdly, if it does have an impossible length less than zero you then call toString() on it again (with no variable to receive the impossible less than zero result) and you then try to set the text of your t1 EditText.
The chances are that your file reading is failing (probably because you only save the strings later in the onClick(...) method). Because your task strings will be null if the file reading fails then you need to test for null before trying to use them.
In other words you're doing this in your code...
String task1 = null;
To fix the bit of code I enclosed at the beginning, use...
if (task1 != null) {
t1.setText(task1);
}
else {
t1.setText(edit);
}
...but most importantly, make sure your files have the strings in them that you need to read.

Categories