将数组复制到另一个数组

时间:2014-11-26 06:14:43

标签: android

我有两个数组,第一个是选项列表,就像这样由json填充

public class Options {
    private String age;
    private String optionNo;
    private String installmentAmount;
    private String noInstallment;

    public String getNoInstallment() {
        return noInstallment;
    }

    public void setNoInstallment(String noInstallment) {
        this.noInstallment = noInstallment;
    }

    public String getInstallmentAmount() {
        return installmentAmount;
    }

    public void setInstallmentAmount(String installmentAmount) {
        this.installmentAmount = installmentAmount;
    }

    public String getOptionNo() {
        return optionNo;
    }

    public void setOptionNo(String optionNo) {
        this.optionNo = optionNo;
    }



second one is templst 




public class TempList {
private String tage;
private String toptionNo;
private int tinstallmentAmount;
private int tnoInstallment;
private String tdescription;
private String tto;
private String tmode;
private String tfrom;

public String getTage() {
    return tage;
}
public void setTage(String tage) {
    this.tage = tage;
}
public String getToptionNo() {
    return toptionNo;
}
public void setToptionNo(String toptionNo) {
    this.toptionNo = toptionNo;
}
public int getTinstallmentAmount() {
    return tinstallmentAmount;
}
public void setTinstallmentAmount(int tinstallmentAmount) {
    this.tinstallmentAmount = tinstallmentAmount;
}
public int getTnoInstallment() {
    return tnoInstallment;
}
public void setTnoInstallment(int tnoInstallment) {
    this.tnoInstallment = tnoInstallment;
}
public String getTdescription() {
    return tdescription;
}
public void setTdescription(String tdescription) {
    this.tdescription = tdescription;
}
public String getTto() {
    return tto;
}
public void setTto(String tto) {
    this.tto = tto;
}
public String getTmode() {
    return tmode;
}
public void setTmode(String tmode) {
    this.tmode = tmode;
}
public String getTfrom() {
    return tfrom;
}
public void setTfrom(String tfrom) {
    this.tfrom = tfrom;
}

}

在我的主要活动中,我希望将一些细节复制到最新的,因为用户输入的年龄这是我的代码

int age=18;          
int amount = 10000;  
int installamt;
int tempIndx=0; 



for(int i=0; i < optionlst.length; i++){
        if (age == Integer.parseInt(optionlst[i].getAge()))
        {
            installamt =  (amount/10000)*(Integer.parseInt(optionlst[i].getInstallmentAmount()));
            templst[tempIndx].setTinstallmentAmount(installamt);
                 templst[tempIndx].setTnoInstallment(Integer.parseInt(optionlst[i].getNoInstallment()));
            tempIndx++ ;    
        }
    } 

但是当我运行程序时,它给错误应用程序意外停止这是我的logcat,如果有些可以请求我的代码中的错误是什么?

 11-26 11:35:36.143: E/AndroidRuntime(656): FATAL EXCEPTION: main
11-26 11:35:36.143: E/AndroidRuntime(656): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.jsonfinal/com.example.jsonfinal.MainActivity}: java.lang.NullPointerException
11-26 11:35:36.143: E/AndroidRuntime(656):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
11-26 11:35:36.143: E/AndroidRuntime(656):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
11-26 11:35:36.143: E/AndroidRuntime(656):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
11-26 11:35:36.143: E/AndroidRuntime(656):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
11-26 11:35:36.143: E/AndroidRuntime(656):  at android.os.Handler.dispatchMessage(Handler.java:99)
11-26 11:35:36.143: E/AndroidRuntime(656):  at android.os.Looper.loop(Looper.java:123)
11-26 11:35:36.143: E/AndroidRuntime(656):  at android.app.ActivityThread.main(ActivityThread.java:3683)
11-26 11:35:36.143: E/AndroidRuntime(656):  at java.lang.reflect.Method.invokeNative(Native Method)
11-26 11:35:36.143: E/AndroidRuntime(656):  at java.lang.reflect.Method.invoke(Method.java:507)
11-26 11:35:36.143: E/AndroidRuntime(656):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
11-26 11:35:36.143: E/AndroidRuntime(656):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
11-26 11:35:36.143: E/AndroidRuntime(656):  at dalvik.system.NativeStart.main(Native Method)
11-26 11:35:36.143: E/AndroidRuntime(656): Caused by: java.lang.NullPointerException
11-26 11:35:36.143: E/AndroidRuntime(656):  at com.example.jsonfinal.MainActivity.onCreate(MainActivity.java:170)
11-26 11:35:36.143: E/AndroidRuntime(656):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-26 11:35:36.143: E/AndroidRuntime(656):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
11-26 11:35:36.143: E/AndroidRuntime(656):  ... 11 more

我的主要活动

import java.io.IOException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.util.EntityUtils;
import org.apache.http.client.*;
import org.apache.http.impl.client.DefaultHttpClient;


import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonParser;

import android.support.v7.app.ActionBarActivity;
import android.net.ParseException;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;


public class MainActivity extends ActionBarActivity {

    HttpClient httpclient1 = new DefaultHttpClient();
    HttpClient httpclient2 = new DefaultHttpClient();
    Packages[] packagelst; 
    Options[] optionlst;
    TempList[] templst;
    int age=18;          
    int amount = 10000;  // to get the expected monthly benefit
    int installamt;
    int tempIndx=0;

    private Thread t1 = null;
    private Thread t2 = null;

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


      t1 =  new Thread(new Runnable() {
            public void run() {


                 try {
                     HttpGet httpGet = new HttpGet("here my web service address");
                    // httpGet.setHeader("Content-Type", "application/json");
                    // httpGet.setHeader("Accept", "JSON");

                     HttpResponse httpResponse = httpclient1.execute(httpGet);
                     HttpEntity resEntity = httpResponse.getEntity();
                     if (resEntity != null) {
                         String resppack = EntityUtils.toString(resEntity);
                         Log.i("Response", resppack);


                         GsonBuilder gsonb = new GsonBuilder();
                         Gson gson = gsonb.create();

                        // EDineOpeningHoursServerResponse serverResponse = null;
                         JsonParser parser = new JsonParser();
                         if (parser.parse(resppack).isJsonArray()) {
                             packagelst = gson.fromJson(resppack, Packages[].class);
                         } else if (parser.parse(resppack).isJsonArray()) {

                         } 

                       //  for(int i = 0; i<packagelst.length; i++)
                       //  {
                       //   Log.i("Option No" , packagelst[i].getOptionNo());        
                       //  }

                        // return serverResponse;
                     }
                     else{ Log.i("TAG", "null");}
                     //return null;
                 } catch (ClientProtocolException e) {
                     e.printStackTrace();
                    // throw new EDineDeliveryException(100, "Error Occurred while processing the request");
                 } catch (IllegalArgumentException e) {
                     e.printStackTrace();
                    // throw new EDineDeliveryException(101, "Invalid user input");
                 } catch (IOException e) {
                     e.printStackTrace();
                   //  throw new EDineDeliveryException(102, ApplicationConstants.CONNECTION_LOST_ERROR_MSG);
                 } catch (ParseException e) {
                     e.printStackTrace();
                    // throw new EDineDeliveryException(103, ApplicationConstants.CONNECTION_LOST_ERROR_MSG);
                 } catch (Exception e) {
                     e.printStackTrace();
                    // throw new EDineDeliveryException(106, "Error Occurred while processing the request");
                 }


            }
          });




        t2 = new Thread(new Runnable() {
            public void run() {


                 try {
                     HttpGet httpGet = new HttpGet("here my web service address");
                    // httpGet.setHeader("Content-Type", "application/json");
                    // httpGet.setHeader("Accept", "JSON");

                     HttpResponse httpResponse = httpclient2.execute(httpGet);
                     HttpEntity resEntity = httpResponse.getEntity();
                     if (resEntity != null) {
                         String resp = EntityUtils.toString(resEntity);
                         Log.i("Response", resp);


                         GsonBuilder gsonb = new GsonBuilder();
                         Gson gson = gsonb.create();

                        // EDineOpeningHoursServerResponse serverResponse = null;
                         JsonParser parser = new JsonParser();
                         if (parser.parse(resp).isJsonArray()) {
                             optionlst = gson.fromJson(resp, Options[].class);
                         } else if (parser.parse(resp).isJsonArray()) {

                         } 

                        // for(int i = 0; i<optionlst.length; i++)
                        // {
                        // Log.i("Age" , optionlst[i].getAge());                         
                       //  }

                        // return serverResponse;
                     }
                     else{ Log.i("TAG", "null");}
                     //return null;
                 } catch (ClientProtocolException e) {
                     e.printStackTrace();
                    // throw new EDineDeliveryException(100, "Error Occurred while processing the request");
                 } catch (IllegalArgumentException e) {
                     e.printStackTrace();
                    // throw new EDineDeliveryException(101, "Invalid user input");
                 } catch (IOException e) {
                     e.printStackTrace();
                   //  throw new EDineDeliveryException(102, ApplicationConstants.CONNECTION_LOST_ERROR_MSG);
                 } catch (ParseException e) {
                     e.printStackTrace();
                    // throw new EDineDeliveryException(103, ApplicationConstants.CONNECTION_LOST_ERROR_MSG);
                 } catch (Exception e) {
                     e.printStackTrace();
                    // throw new EDineDeliveryException(106, "Error Occurred while processing the request");
                 }


            }
          });


        t1.start();
        t2.start();


        for(int i=0; i < optionlst.length; i++){
            if (age == Integer.parseInt(optionlst[i].getAge()))
            {
                installamt =  (amount/10000)*(Integer.parseInt(optionlst[i].getInstallmentAmount()));
                templst[tempIndx].setTinstallmentAmount(installamt);
                templst[tempIndx].setTnoInstallment(Integer.parseInt(optionlst[i].getNoInstallment()));
                tempIndx++ ;    
            }
        }   
      /*  
            tempIndx = 0;

        for(int k=0; k < optionlst.length; k++){
                 templst[k].setTto(packagelst[k].getTo());
                 templst[k].setTfrom(packagelst[k].getFrom());
                 templst[k].setTmode(packagelst[k].getMode());


             }
       */









       // opp("http://172.20.106.125:8080/BOCService/webresources/abhimana/packages");
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }



}

3 个答案:

答案 0 :(得分:0)

您正在尝试从templst数组访问数据,该数据在从中访问数据之前从不进行初始化。

答案 1 :(得分:0)

初始化变量templst:

<强>像:

TempList[] templst = new TempList[size];

ArrayList<TempList> templst = new ArrayList<TempList>();

答案 2 :(得分:0)

你的数组templst永远不会被初始化。你可以在循环之前初始化它.Arrays需要一个固定的大小来实例化。