Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Getting information from an array

  1. #1
    Junior Member
    Join Date
    May 2011
    Posts
    4
    My Mood
    Bored
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Getting information from an array

    I have an array of 3 rows and 4 columns. I need to compare every element of it with the others to find if it is repeated, how many times it repeats and the number of the row where they appear. I tried to use 4 for statements, and saving each one of these arguments in a class by making comparisons, but it doesn't work because every time I try to run the program this error appears java.lang.NullPointerException.

    Thanks for your help in advance.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Getting information from an array

    Sounds like you didn't initialize your array properly. Please post your code, otherwise it's difficult for us to see what's wrong.

  3. #3
    Junior Member
    Join Date
    May 2011
    Posts
    4
    My Mood
    Bored
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Getting information from an array

    This is the complete code.

    /************************************************** ***************
    JADE - Java Agent DEvelopment Framework is a framework to develop
    multi-agent systems in compliance with the FIPA specifications.
    Copyright (C) 2000 CSELT S.p.A.

    GNU Lesser General Public License

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation,
    version 2.1 of the License.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the
    Free Software Foundation, Inc., 59 Temple Place - Suite 330,
    Boston, MA 02111-1307, USA.
    ************************************************** ***************/

    package pingAgent;

    import java.io.*;
    import java.lang.*;
    import java.lang.reflect.Array;
    import java.sql.Struct;

    import mensaje.Mensaje;
    import jade.core.*;
    import jade.core.Service.Slice;
    import jade.core.behaviours.*;
    import jade.lang.acl.ACLMessage;
    import jade.domain.FIPAAgentManagement.ServiceDescription ;
    import jade.domain.FIPAAgentManagement.DFAgentDescription ;
    import jade.domain.DFService;
    import jade.domain.FIPAException;
    import jade.util.Logger;
    import jade.util.leap.ArrayList;

    /**
    * This agent implements a simple Ping Agent that registers itself with the DF and
    * then waits for ACLMessages.
    * If a REQUEST message is received containing the string "ping" within the content
    * then it replies with an INFORM message whose content will be the string "pong".
    *
    * @author Tiziana Trucco - CSELT S.p.A.
    * @version $Date: 2010-04-08 13:08:55 +0200 (gio, 08 apr 2010) $ $Revision: 6297 $
    */

    public class ReceiverAgent extends Agent {
    public class encuentrarepeticion{
    int temporal;
    int[] temporal1;
    int numerorep;
    };

    private static final long serialVersionUID = 1L;
    private Logger myLogger = Logger.getMyLogger(getClass().getName());
    private class WaitPingAndReplyBehaviour extends CyclicBehaviour {


    private static final long serialVersionUID = 1L;

    public WaitPingAndReplyBehaviour(Agent a) {
    super(a);
    }

    public void action() {
    try{
    ACLMessage msg = myAgent.receive();
    if(msg != null){
    File directorio= new File(".");
    String lista[]=directorio.list(new filtro());
    int temp1=0,temp2=0,temp3 = 0,temp4=0,temp5=0,temp6=0,temp10=0,temp11=0,temp12 =0,temp13=0, tmp2=0;
    int pos1a=0,pos1b=0,pos2a=0,pos2b=0,pos3a=0,pos3b=0,po s4a=0,pos4b=0,pos5a=0,pos5b=0,pos6a=0,pos6b=0,pos7 a=0,pos7b=0,pos8a=0,pos8b=0,pos9a=0,pos9b=0,pos10a =0,pos10b=0;
    int[] eldm1={0,0,0,0,0},eldm2={0,0,0,0,0},eldm3={0,0,0,0 ,0},eldm4={0,0,0,0,0},eldm5={0,0,0,0,0},eldm6={0,0 ,0,0,0},eldm7={0,0,0,0,0},eldm8={0,0,0,0,0},eldm9= {0,0,0,0,0},eldm10={0,0,0,0,0},eldm11={0,0,0,0,0}, eldm12={0,0,0,0,0};
    double maxsum=1000000;
    double sum12=0;
    double dimestimado=0;
    double severidad=0;
    int[] Dimension={30,7,7};
    int[] damage={30,7,7};
    int[] eld1={30,7,7};
    int[] eld2={30,7,7};
    int[] eld3={30,7,7};
    int[] eld4={30,7,7};
    int[][] matrizeld=new int[3][4];
    String[] piso = {"e","t","d"};
    String[] clase = {"a","a","d"};
    String[] porcentajedano= {"e","t","d"};
    String[] elementodanado1= {"e","t","d"};
    String[] elementodanado2= {"e","t","d"};
    String[] elementodanado3= {"e","t","d"};
    String[] elementodanado4= {"e","t","d"};
    String mde="";
    encuentrarepeticion encrpt[] = new encuentrarepeticion[100];
    Mensaje msj = (Mensaje)msg.getContentObject();
    /*ArrayList archivos= new ArrayList();
    archivos.add("daño2elementos20.txt");
    archivos.add("daño2elementos30.txt");
    archivos.add("daño2elementos40.txt");
    archivos.add("daño2elementos50.txt");*/
    try
    {
    double[] sumt=new double[lista.length];
    for(int i=0;i<lista.length;++i) //ciclo para comparar la información del mensaje con la información almacenada
    {
    if(msj.getArchivo().compareTo(lista[i].toString())!=0)
    {
    BufferedReader bf = new BufferedReader(new FileReader(lista[i]));
    String linea=null;
    ArrayList resultado = new ArrayList();
    while ((linea = bf.readLine())!=null)
    resultado.add(Double.parseDouble(linea));
    double suma = 0;
    for(int j=0;j<resultado.size();++j)
    {
    //System.out.print(resultado.get(i));
    double aj=msj.getArreglo()[j];
    double bj=Double.parseDouble(resultado.get(j).toString()) ;
    suma=suma+Math.pow(aj-bj,2);
    if(suma<maxsum){
    maxsum=suma;
    mde = lista[i].toString();
    }
    }
    sumt[i]=Math.sqrt(suma);
    //System.out.println(sumt[i]);
    }
    else {
    sumt[i]=1000000;
    }
    }
    //ordenamiento del vector sumt
    int b,j;
    double asd,t;
    double index[]= new double[sumt.length];
    for (int sd=0;sd<sumt.length;sd++){
    index[sd]=sd;
    }
    do
    {
    b=0;
    for(j=0;j<sumt.length-1;j++)
    {
    if(sumt[j]>sumt[j+1]){
    asd=index[j];
    index[j]=index[j+1];
    index[j+1]=asd;
    t=sumt[j];
    sumt[j]=sumt[j+1];
    sumt[j+1]=t;
    b++;
    }
    }
    }
    while(b>0);
    for(int i=0;i<sumt.length;++i){
    // System.out.println(sumt[i]);
    //System.out.println(index[i]);
    }
    System.out.println("El primer archivo mas cercano al caso es \t" + lista[(int) index[0]].toString());
    System.out.println("El segundo archivo mas cercano al caso es \t" + lista[(int) index[1]].toString());
    System.out.println("El tercer archivo mas cercano al caso es \t" + lista[(int) index[2]].toString());
    double send1= sumt[0];
    double send2= sumt[1];
    double send3= sumt[2];

    int fs=sumt.length;
    System.out.println(send1 + "\t" + send2 + "\t" + send3);
    for (int k=0;k<3;++k){
    //String[] piso = lista;
    piso[k]=lista[(int) index[k]].toString().substring(4,5);
    //System.out.println(piso[k]);
    //String[] clase = lista;
    clase[k]=lista[(int) index[k]].toString().substring(11,12);
    //System.out.println(clase[k]);
    //String[] porcentajedano= lista;
    porcentajedano[k]=lista[(int) index[k]].toString().substring(34,37);
    //System.out.println(porcentajedano[k]);
    //String[] elementodanado1= lista;
    elementodanado1[k]=lista[(int) index[k]].toString().substring(55,57);
    //System.out.println(elementodanado1[k]);
    //String[] elementodanado2= lista;
    elementodanado2[k]=lista[(int) index[k]].toString().substring(58,60);
    //System.out.println(elementodanado2[k]);
    //String[] elementodanado3= lista;
    elementodanado3[k]=lista[(int) index[k]].toString().substring(61,63);
    //System.out.println(elementodanado3[k]);
    //String[] elementodanado4= lista;
    elementodanado4[k]=lista[(int) index[k]].toString().substring(64,66);
    //System.out.println(elementodanado4[k]);
    }
    for (int l=0;l<3;++l){
    if (sumt[l]==0){
    sumt[l]=0.00000000000000001;
    }
    sum12= (sumt[0]/sumt[l]+sum12);
    Dimension[l]=Integer.parseInt(clase[l])-1;
    damage[l]=Integer.parseInt(porcentajedano[l]);
    eld1[l]=Integer.parseInt(elementodanado1[l]);
    eld2[l]=Integer.parseInt(elementodanado2[l]);
    eld3[l]=Integer.parseInt(elementodanado3[l]);
    eld4[l]=Integer.parseInt(elementodanado4[l]);
    //System.out.println(damage[l]);
    }
    for(int i=0;i<3;i++){
    matrizeld[i][0]=eld1[i];
    matrizeld[i][1]=eld2[i];
    matrizeld[i][2]=eld3[i];
    matrizeld[i][3]=eld4[i];
    }

    for(int c=0;c<4;c++){
    for(int v=0;v<3;v++){
    tmp2=matrizeld[v][c];
    int contador=0;
    int contador2=0;
    int contador3=0;
    for(int w=0;w<4;w++){
    for(int k=0;k<3;k++){
    if(tmp2==matrizeld[k][w]){
    contador=contador+1;
    encrpt[contador2].temporal=tmp2;
    //System.out.print(encrpt[0].temporal);
    //encrpt[contador2].numerorep=contador;
    //encrpt[contador2].temporal1[contador3]=w;
    //contador3=contador3+1;
    }
    }
    }
    contador2=contador2+1;
    }
    }


    for (int l=0;l<3;l++){
    dimestimado=Dimension[l]*(sumt[0]/sumt[l])/sum12 + dimestimado;
    severidad=damage[l]*(sumt[0]/sumt[l])/sum12 + severidad;
    //System.out.println(dimestimado +"__"+ severidad +"_"+eld1[l]+"_"+eld2[l]+"_"+eld3[l]+"_"+eld4[l]);
    }
    dimestimado = Math.round(dimestimado);

    /*for (int i=0;i<3;i++){
    for(int k=i;k<3;k++){
    if(i!=k){
    if(eld1[i]==eld1[k]){
    temp10=temp10+1;
    eldm1=eld1;
    pos1a=i;pos1b=k;
    System.out.println(eld1[i]+"_"+i+"_"+k);
    }
    if(eld2[i]==eld2[k]){
    temp11=temp11+1;
    eldm2=eld2;
    pos2a=i;pos2b=k;
    System.out.println(eld2[i]+"_"+i+"_"+k);
    }
    if(eld3[i]==eld3[k]){
    temp12=temp12+1;
    eldm3=eld3;
    pos3a=i;pos3b=k;
    System.out.println(eld3[i]+"_"+i+"_"+k);
    }
    if(eld4[i]==eld4[k]){
    temp13=temp13+1;
    eldm4=eld4;
    pos4a=i;pos4b=k;
    System.out.println(eld4[i]+"_"+i+"_"+k);
    }
    }
    if(eld1[i]==eld2[k]){
    temp1=temp1+1;
    eldm5=eld1;
    pos5a=i;pos5b=k;
    System.out.println(eld1[i]+"_"+i+"_"+k);
    }
    if(eld1[i]==eld3[k]){
    temp2=temp2+1;
    eldm6=eld1;
    pos6a=i;pos6b=k;
    System.out.println(eld1[i]+"_"+i+"_"+k);
    }
    if(eld1[i]==eld4[k]){
    temp3=temp3+1;
    eldm7=eld1;
    pos7a=i;pos7b=k;
    System.out.println(eld1[i]+"_"+i+"_"+k);
    }
    if(eld2[i]==eld3[k]){
    temp4=temp4+1;
    eldm8=eld2;
    pos8a=i;pos8b=k;
    System.out.println(eld2[i]+"_"+i+"_"+k);
    }
    if(eld2[i]==eld4[k]){
    temp5=temp5+1;
    eldm9=eld2;
    pos9a=i;pos9b=k;
    System.out.println(eld2[i]+"_"+i+"_"+k);
    }
    if(eld3[i]==eld4[k]){
    temp6=temp6+1;
    eldm10=eld3;
    pos10a=i;pos10b=k;
    System.out.println(eld3[i]+"_"+i+"_"+k);
    }
    }
    }

    System.out.println(temp1 +"_"+ temp2 +"_"+ temp3+"_" + temp4 +"_"+ temp5 +"_"+ temp6 +"_"+ temp10 +"_"+ temp11 +"_"+ temp12 +"_"+ temp13);*/


    //System.out.println(sumt[fs-2]);
    //System.out.println(sumt[fs-3]);
    //System.out.println(sumt[fs-4]);
    //System.out.println(archivos.get(i));
    System.out.println("El archivo con la menor distancia euclidiana es: " + mde);
    }
    catch(Exception e){
    System.out.println(e);
    }
    }
    else {
    block();
    }
    }
    catch(Exception e){
    System.out.println(e);
    }

    /*BufferedReader bf = null;
    try {
    bf = new BufferedReader(new FileReader("daño2elementos40.txt"));
    } catch (FileNotFoundException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
    }
    try{
    ACLMessage msg = myAgent.receive();
    if(msg != null){
    Mensaje msj = (Mensaje)msg.getContentObject();
    //System.out.println(msg.getSender());
    //System.out.println(msj.getBuffer());
    for(int i=0;i<msj.getArreglo().length;++i){
    //System.out.println(msj.getArreglo()[i]);
    }
    //double comparacion[]= {2,3,3,4,4,4};
    String linea=null;
    ArrayList resultado = new ArrayList();
    while ((linea = bf.readLine())!=null)
    resultado.add(Double.parseDouble(linea));
    double suma = 0;
    for(int i=0;i<resultado.size();++i)
    {
    //System.out.print(resultado.get(i));
    double ai=msj.getArreglo()[i];
    double bi=Double.parseDouble(resultado.get(i).toString()) ;
    suma=suma+Math.pow(ai-bi,2);
    }
    System.out.println(Math.sqrt(suma));
    }
    else {
    block();
    }
    }catch(Exception e){
    System.out.println(e);
    } */
    }
    } // END of inner class WaitPingAndReplyBehaviour



    protected void setup() {
    // Registration with the DF
    DFAgentDescription dfd = new DFAgentDescription();
    ServiceDescription sd = new ServiceDescription();
    sd.setType("ReceiverAgent");
    sd.setName(getName());
    sd.setOwnership("TILAB");
    dfd.setName(getAID());
    dfd.addServices(sd);
    System.out.println("Agent "+getLocalName()+" started.");
    System.out.println("Receiver agent started.");
    try {
    DFService.register(this,dfd);
    WaitPingAndReplyBehaviour PingBehaviour = new WaitPingAndReplyBehaviour(this);
    addBehaviour(PingBehaviour);

    } catch (FIPAException e) {
    myLogger.log(Logger.SEVERE, "Agent "+getLocalName()+" - Cannot register with DF", e);
    doDelete();
    }
    }
    }

Similar Threads

  1. I cannot get to print out all my information.
    By kl2eativ in forum What's Wrong With My Code?
    Replies: 4
    Last Post: January 25th, 2011, 01:46 PM
  2. taking information from one array into another
    By Stn in forum Object Oriented Programming
    Replies: 1
    Last Post: January 18th, 2011, 11:56 AM
  3. can't get all the information out of my array at once... please help
    By Tate in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 28th, 2010, 06:22 AM
  4. [SOLVED] Class not passing information
    By DiPep in forum What's Wrong With My Code?
    Replies: 6
    Last Post: July 26th, 2010, 11:04 AM
  5. Getting information from a folder
    By shadihrr in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: February 23rd, 2010, 04:13 PM