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 4 of 4

Thread: Assigning variable to an Array

  1. #1
    Junior Member
    Join Date
    Aug 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Assigning variable to an Array

    Hi Experts,
    I have a problem in my code. When iam assigning a avariable to an array it is showing a NullPointerException .Why it is showing that type of exception. This is my code.

    for(int i=0;i<countorder;i++)
    {
    noOfLineItemsFullFilled=0;
    actualShortsupplyItems=0;
    for(int k=0;k<countorder;k++)
    {
     
         if(Ortemp[i].equals(Or[k]))
             value=k;
     }
        for( int j=0;j<stockcount;j++)
        {
     
           // totallinecount++;
            //System.out.println("totalcount"+totallinecount);
            if(oqtymatrix[value][j]!=0 )
            {
              //  noOfLineItemsFullFilled=0;
            if(oqtymatrix[value][j] == FinalAllocation[i][j+1])
            {
     
            noOfLineItemsFullFilled++;
     
                      }
     
     
      }
     
    //System.out.println("LineFillRtae"+(linefill ));
    }
      //System.out.println("total lineItems in an order  "+NoofItemsinOrder[value][0]);System.out.println("Countorder"+countorder);
    linefill =     (NoofItemsinOrder[value][1]+noOfLineItemsFullFilled) * 100/NoofItemsinOrder[value][0];
    System.out.println("LinefillRate"+BigDecimal.valueOf(linefill));
    BigDecimal LFR=BigDecimal.valueOf(linefill);
     
    //LineFillRate[value]=LFR;
     
    //System.out.println("value"+value);
    try
    {
    LineFillRate[value]=LFR;
    System.out.println("LinefillRate try"+LineFillRate[value]);
    }
    catch(NullPointerException e)
    {
    //System.out.println("LinefillRate"+LineFillRate[value]);
    }
    }


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Assigning variable to an Array

    Hello sridevi. Welcome to the Java Programming Forums.

    Where abouts in the code is the null pointer exception being generated?

    Can you please post all of your code?
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  3. #3
    Junior Member
    Join Date
    Aug 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Assigning variable to an Array

    Hi,
    This is my full code.
    /*
    * To change this template, choose Tools | Templates
    * and open the template in the editor.
    */




    import java.io.*;
    import java.math.BigDecimal;
    import java.math.MathContext;
    import java.sql.*;


    public class LinFillRate {

    public static void main(String args[]) throws IOException {

    Connection con=null;
    PreparedStatement pst=null;
    PreparedStatement pst1=null;
    ResultSet rs=null;
    String Materials;
    BigDecimal Stk_hnd;
    String Orders;
    String vbapMatnr;
    BigDecimal oqty;
    String[] Or;
    double[][] oqtymatrix;
    int countorder=0;
    int orderinc=0;
    int m=0;
    int stockcount=0;
    int shortsupplyinc=0;
    int stockonhandinc=0;
    String[] shortsupplyitems; // matnr in stockonhandtable.
    double[] stockonhnd;
    double[][] DivideOrderQuan;
    double[][] FinalAllocation;
    double[] rowweight;
    double[] rowweighttemp;
    String[] Ortemp;
    double temp = 0;
    String a = null;
    String b = null;
    int filledorders=0;
    int totalorders=0;
    double OrderfillRate=0;
    String Item = null;
    //boolean isorderfilled=false;
    int fillcount=0;
    int totallinecount=0;
    String temporary ;
    String ItemMatrix[]=null ;
    int iteminc=0;
    int itemcount=0;
    MathContext mc = null;
    boolean isorderfilled = false;
    String Extid;
    int nonShortsupplyLineItems ;
    int lineItemsCount = 0;
    int TotallineItemsCount = 0;
    int noOfLineItemsinOrder;
    int[][] NoofItemsinOrder;
    int actualShortsupplyItems = 0;
    double[][] lineFillRate=new double[countorder][1] ;
    PreparedStatement pst2=null;
    Connection Fcon=null;
    ResultSet rsF=null;
    int count;
    int value = 0;
    int[] linefillrate = new int[countorder];
    double linefill ;
    BigDecimal[] LineFillRate = null;
    String extractid;
    String mandt;
    String vbeln;
    try
    {


    Class.forName("com.mysql.jdbc.Driver");
    con =DriverManager.getConnection ("jdbc:mysql://localhost:3306/shortsupply",
    "root", "rvst");
    // System.out.println("Connection established");

    pst=con.prepareStatement("select * from header");
    rs=pst.executeQuery();
    while(rs.next())
    {
    countorder++;
    }
    // System.out.println("vbakcountorder"+countorder);
    Or=new String[countorder];
    pst=con.prepareStatement("select * from material");
    rs=pst.executeQuery();
    while(rs.next())
    {
    stockcount++;
    }

    shortsupplyitems=new String[stockcount];
    stockonhnd=new double[stockcount];
    pst=con.prepareStatement("select * from material");
    rs=pst.executeQuery();
    while(rs.next())
    {
    Materials= rs.getString(3);
    Stk_hnd=rs.getBigDecimal(4);

    shortsupplyitems[shortsupplyinc]=Materials;
    //System.out.println(" "+shortsupplyitems[i]);

    stockonhnd[stockonhandinc]=Stk_hnd.doubleValue() ;
    shortsupplyinc++;
    stockonhandinc++;

    //System.out.println(" "+stockonhnd[i]);

    }
    System.out.println("StockonHand Before Optimization");
    for(int i=0; i<stockcount;i++)
    {
    System.out.print(shortsupplyitems[i]);
    System.out.println(" "+stockonhnd[i]);
    }

    pst=con.prepareStatement("select * from header");
    rs=pst.executeQuery();
    while(rs.next())
    {
    Orders= rs.getString(3);

    // System.out.println(n);
    // for(int i=0;i<countorder;i++){
    Or[orderinc]=Orders;
    orderinc++;
    // System.out.println(""+Or[i]);

    }




    System.out.println();



    oqtymatrix=new double[countorder][stockcount];
    NoofItemsinOrder=new int[countorder][2];

    String s;

    // System.out.println("n"+n);
    //System.out.println("m"+m);
    for(int i=0;i<countorder;i++)
    {
    a=Or[i];
    noOfLineItemsinOrder = 0;
    nonShortsupplyLineItems = 0;

    //a is Orders in vbak table
    for(int j=0;j<stockcount;j++)
    {
    b=shortsupplyitems[j];//b is matnr in stockonhand table
    pst=con.prepareStatement("select * from item where vbeln='"+a+"' and matnr='"+b+"' ");
    rs=pst.executeQuery();

    if(rs.next())
    {
    noOfLineItemsinOrder++;
    oqty=rs.getBigDecimal(13);
    s=rs.getString(14);
    oqtymatrix[i][j]=oqty.doubleValue();
    if(!s.equals("c"))
    {
    nonShortsupplyLineItems++;
    }


    // System.out.println(" "+oqtymatrix[i][j]);
    // countorder++;
    // stockcount++;
    }
    else
    {
    //oqty=null;
    oqtymatrix[i][j]=0;
    }

    }
    NoofItemsinOrder[i][0]= noOfLineItemsinOrder;
    NoofItemsinOrder[i][1]= nonShortsupplyLineItems;

    //System.out.println("LineItemDetails nonShortsupplyLineItems "+NoofItemsinOrder[i][1]);
    }


    System.out.println("OpenQuantities");
    System.out.print("Orders ");
    for (int i = 0; i < stockcount; i++) {
    System.out.print(" " + shortsupplyitems[i]);

    }
    System.out.println();
    for(int i=0;i<countorder;i++)
    {
    System.out.print(Integer.parseInt(Or[i]));
    for(int j=0;j<stockcount;j++)
    {
    System.out.print(" "+oqtymatrix[i][j]);
    }
    System.out.println();
    }
    double div;
    DivideOrderQuan=new double[countorder][stockcount];
    System.out.println("dividing both orders and Quantities:");
    for (int i = 0; i < countorder; i++) {
    for (int j = 0; j <stockcount; j++) {
    try{
    div=oqtymatrix[i][j] / stockonhnd[j];
    if (Double.isInfinite(div))
    {
    div=0.0;
    }
    }
    catch(NullPointerException e)
    {
    div=0.0;
    }
    // div=oqtymatrix[i][j].divide( stockonhnd[j]).doubleValue() ;


    DivideOrderQuan[i][j]=div;
    System.out.print(" "+DivideOrderQuan[i][j]);
    }
    System.out.println();
    }
    rowweight=new double[countorder];
    rowweighttemp=new double[countorder];

    //Calculating the row weight
    for (int i = 0; i < countorder; i++) {
    rowweight[i] = 0.0;
    for (int j = 0; j < stockcount; j++) {
    rowweight[i] = rowweight[i] + DivideOrderQuan[i][j];

    }
    // System.out.println("rowweight"+rowweight[i]);
    }
    //copying the rowweight values into rowweighttemp
    for (int i = 0; i < countorder; i++) {
    rowweighttemp[i] = rowweight[i];
    }
    Ortemp=new String[countorder];
    for (int i = 0; i < countorder; i++) {
    Ortemp[i] = Or[i];
    }

    //Sorting the rowweight
    for (int i = 0; i < countorder; i++) {
    for (int j = 0; j < i; j++) {
    if (rowweight[i] < rowweight[j]) {
    temp = rowweight[i];
    rowweight[i] = rowweight[j];
    rowweight[j] = temp;
    temporary=Ortemp[i];
    Ortemp[i]=Ortemp[j];
    Ortemp[j]=temporary;

    }
    }
    }

    System.out.println("Rowweight");
    for(int i=0;i<countorder;i++)
    System.out.println(rowweight[i]);
    System.out.println("Salesorders sorting");
    for(int i=0;i<countorder;i++)
    System.out.println(Ortemp[i]);
    FinalAllocation=new double[countorder][stockcount+1];

    for (int i = 0; i < countorder; i++) {
    // double temp1 = Ortemp[i];
    // double temp1 = i+1;
    //int temp2 = (int) temp1;
    for(int k=0;k<countorder;k++)
    {

    if(Ortemp[i].equals(Or[k]))
    value=k;
    }
    //System.out.println("k value"+Or[k]);

    FinalAllocation[i][0] =Double.parseDouble( Ortemp[i]);
    // Checking the Orders are less than are equal to Quantities if the condition is true incrementing the count
    count=0;
    for (int j = 0; j < stockcount; j++) {
    try{
    if (oqtymatrix[value][j] <= stockonhnd[j]) {
    // System.out.println("k value "+k);
    // System.out.println("oqtycheck"+oqtymatrix[value][j]);
    // System.out.println("stockonhand"+stockonhnd[j]);
    count++;
    }
    }

    catch(NullPointerException e)
    {

    // oqtymatrix[temp2 - 1][j]=null;
    //stockonhnd[j]=null;
    }
    }
    // System.out.println("count"+count);
    //System.out.println("countorder"+countorder);

    //If the countis 4 allocate the FinalAllocation Matrix with the Orders and decrement the value in the Quantities
    if (count == stockcount) {
    for (int j = 0; j < stockcount; j++) {
    FinalAllocation[i][j + 1] = oqtymatrix[value][j];
    // System.out.println("enetring loop");
    // System.out.println("oqtymatrixfinalallocation"+Fin alAllocation[i][j + 1]);
    //

    stockonhnd[j] = stockonhnd[j]-oqtymatrix[value][j] ;
    // stockonhnd[j] = stockonhnd[j].subtract(oqtymatrix[temp2-1]);
    // System.out.println("Stockonhand"+stockonhnd[j]);
    }
    }
    else
    {
    for (int j = 0; j < stockcount; j++) {
    FinalAllocation[i][j + 1] =0;

    }
    }
    }





    System.out.println();
    System.out.println("Allocation After Optimization[Modified Quantities]" + " ");
    System.out.print(" Orders ");
    for (int i = 0; i < stockcount; i++) {
    System.out.print(" " + shortsupplyitems[i]);
    }
    System.out.println();
    for (int i = 0; i <countorder ; i++) {

    for (int j = 0; j < stockcount+1; j++) {
    // FinalAllocation[0][j+1]=shortsupplyitems[j];
    System.out.print(" " + FinalAllocation[i][j]);
    }
    System.out.println();
    }

    System.out.println();
    System.out.println("StockonHand After Optimization" + " ");
    for (int i = 0; i < stockcount; i++) {
    System.out.print(shortsupplyitems[i]);
    System.out.println( " "+stockonhnd[i]);
    }



    int noOfLineItemsFullFilled=0;
    for(int i=0;i<countorder;i++)
    {
    boolean orderfullfilled=true;
    totalorders++;
    //actualShortsupplyItems=0;
    for(int k=0;k<countorder;k++)
    {

    if(Ortemp[i].equals(Or[k]))
    value=k;
    }
    for( int j=0;j<stockcount;j++)
    {

    // totallinecount++;
    //System.out.println("totalcount"+totallinecount);
    if(oqtymatrix[value][j] == FinalAllocation[i][j+1])
    {



    }
    else
    {
    orderfullfilled=false;
    }

    }

    if(orderfullfilled)
    {
    filledorders++;

    }

    }














    //System.out.println("actualshortsupplyitems"+actual ShortsupplyItems);
    System.out.println("filledorders "+filledorders);



    OrderfillRate= (filledorders *100) / totalorders;
    //OrderfillRate= (noOfLinesItemsFullFilled *100) / totalorders;
    System.out.println("Post Optimized Orderfillrate " +OrderfillRate+"%");

    /*pst=con.prepareStatement("select * from header");
    rs=pst.executeQuery();
    while(rs.next())
    {
    extractid=rs.getString(1);
    mandt=rs.getString(2);
    vbeln=rs.getString(3);

    String linefil="insert into LineFillRate values(?,?,?,?)";
    pst1=con.prepareStatement(linefil);
    pst1.setString(1, extractid);
    pst1.setString(2,mandt);
    pst1.setString(3,vbeln);
    pst1.setBigDecimal(4,null);
    pst1.executeUpdate();
    }*/








    for(int i=0;i<countorder;i++)
    {
    noOfLineItemsFullFilled=0;
    actualShortsupplyItems=0;
    for(int k=0;k<countorder;k++)
    {

    if(Ortemp[i].equals(Or[k]))
    value=k;
    }
    for( int j=0;j<stockcount;j++)
    {

    // totallinecount++;
    //System.out.println("totalcount"+totallinecount);
    if(oqtymatrix[value][j]!=0 )
    {
    // noOfLineItemsFullFilled=0;
    if(oqtymatrix[value][j] == FinalAllocation[i][j+1])
    {

    noOfLineItemsFullFilled++;

    }


    }



    //System.out.println("LineFillRtae"+(linefill ));
    }
    //System.out.println("total lineItems in an order "+NoofItemsinOrder[value][0]);System.out.println("Countorder"+countorder);
    linefill = (NoofItemsinOrder[value][1]+noOfLineItemsFullFilled) * 100/NoofItemsinOrder[value][0];
    System.out.println("LinefillRate"+BigDecimal.value Of(linefill));
    BigDecimal LFR=BigDecimal.valueOf(linefill);

    //LineFillRate[value]=LFR;








    //System.out.println("value"+value);
    try
    {
    LineFillRate[value]=LFR;
    System.out.println("LinefillRate try"+LineFillRate[value]);
    }
    catch(NullPointerException e)
    {
    //System.out.println("LinefillRate"+LineFillRate[value]);
    }




    /* String extlf="select extractid from linefillrate";
    pst=con.prepareStatement(extlf);
    rs=pst.executeQuery();
    if(rs.next())
    {
    String extract=rs.getString(1);
    String updateline="update linefillRate set linefill ='"+LFR+"' where extractid='"+extract+"'";
    PreparedStatement pstlf=Fcon.prepareStatement(updateline);

    // pst.setBigDecimal(10,moqty);
    pstlf.executeUpdate();*/












    /*pst=con.prepareStatement("select * from header");
    rs=pst.executeQuery();
    while(rs.next())
    {
    extractid=rs.getString(1);
    mandt=rs.getString(2);
    vbeln=rs.getString(3);

    String linefil="insert into LineFillRate values(?,?,?,?)";
    pst1=con.prepareStatement(linefil);
    pst1.setString(1, extractid);
    pst1.setString(2,mandt);
    pst1.setString(3,Ortemp[i]);
    pst1.setBigDecimal(4,BigDecimal.valueOf(linefill)) ;
    pst1.executeUpdate();*/



    //System.out.println("Succesfully inserted linefillrate table");
    //}


    }























    try
    {


    Class.forName("com.mysql.jdbc.Driver");
    Fcon =DriverManager.getConnection ("jdbc:mysql://localhost:3306/shortsupply",
    "root", "rvst");
    System.out.println("connection established in update");

    String ext="select extractid from orderfillRate";
    pst2=Fcon.prepareStatement(ext);
    rsF=pst2.executeQuery();
    if(rsF.next())
    {
    String extract=rsF.getString(1);
    String update="update orderfillRate set postoptimised ='"+OrderfillRate+"' where extractid='"+extract+"'";
    PreparedStatement pst3=Fcon.prepareStatement(update);

    // pst.setBigDecimal(10,moqty);
    pst3.executeUpdate();

    System.out.println("Successfully insertedorderfillrate");
    }
    else
    {
    System.out.println("not updated");
    }
    }
    catch(ClassNotFoundException e)
    {
    System.out.println("could not load database driver"+e.getMessage());
    }
    catch(SQLException e)
    {
    System.out.println("Sql exception caught"+e.getMessage());
    //ReceiveOperation=false;
    }
    finally
    {
    try
    {
    if(Fcon != null)
    Fcon.close();
    }
    catch(SQLException ignored)
    {
    System.out.println(ignored);
    }


    }













    for(int i=0;i<countorder;i++)
    {
    a=Ortemp[i];
    //--------------------------------------------------noOfLineItemsinOrder = 0;
    //System.out.println("Order"+a);
    for(int k=0;k<countorder;k++)
    {

    if(Ortemp[i].equals(Or[k]))
    value=k;
    // a=Or[k];
    // System.out.println("Ordernoineer"+a);
    }
    // int c=(int)Ortemp[i];
    //a= Integer.toString(c);

    System.out.println("Orderno"+a);
    for( int j=0;j<stockcount;j++)
    {
    b=shortsupplyitems[j];
    if(oqtymatrix[value][j]!=0)
    {

    String itemRet="select * from item where vbeln='"+a+"' and matnr='"+b+"'";
    pst=con.prepareStatement(itemRet);
    rs= pst.executeQuery();


    if(rs.next())
    {
    // -- ---------------------------------------------------------------- noOfLineItemsinOrder++;
    Extid=rs.getString(1);
    Item=rs.getString(4);

    // ItemMatrix[k]=Item;

    //iteminc++;
    // System.out.println("Iner Itemno"+Item);








    //System.out.println("outer Itemno"+Item);

    String moqtyinsert="insert into scheduleline values(?,?,?,?,?,?,?,?,?,?,?)";
    pst1=con.prepareStatement(moqtyinsert);
    BigDecimal moquantity=BigDecimal.valueOf(FinalAllocation[i][j+1]);
    pst1.setString(1,Extid );
    pst1.setString(2, "800");
    pst1.setString(3, a);
    // System.out.println("Orderno"+a);

    pst1.setString(4,Item);
    //System.out.println("Item no"+Item);

    pst1.setString(5, "9999");
    pst1.setDate(6, null);
    pst1.setDate(7,null);
    pst1.setBigDecimal(8,null);

    pst1.setBigDecimal(9, moquantity);
    System.out.println("modified quantities"+moquantity);
    pst1.setString(10, "ST");
    pst1.setString(11, "0");
    pst1.executeUpdate();
    //System.out.println("Inserted vbeptemp");

    }

    }
    }
    System.out.println("LineItemDetails noOfLineItemsinOrder "+NoofItemsinOrder[i][0]);
    //-------------------------------------------------------------------- NoofItemsinOrder[i][0]= noOfLineItemsinOrder;
    }
    //}
    }


    catch(ClassNotFoundException e)
    {
    System.out.println("could not load database driver"+e.getMessage());
    }
    catch(SQLException e)
    {
    System.out.println("Sql exception caught"+e.getMessage());
    //ReceiveOperation=false;
    }
    finally
    {
    try
    {
    if(con != null)
    con.close();
    }
    catch(SQLException ignored)
    {
    System.out.println(ignored);
    }

    try
    {
    pst.close();
    }
    catch(SQLException ignored)
    {
    System.out.println(ignored);
    }


    }

















    }


    }

  4. #4
    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: Assigning variable to an Array

    ooh, so messy. when you declared LineFillRate, you initialized it to null. By initializing it to an array of BigDecimal's with 1 element, it compiles, but i still don't know what your program is suppose to do, and it still doesn't run (unless it's suppose to throw a null pointer exception).

    Try using helper methods. They'll really clean up your code and will make debugging a lot easier. Also, more comments that explained what the code is doing would be helpful.

Similar Threads

  1. Java Variable Conversions
    By JavaPF in forum Java Programming Tutorials
    Replies: 2
    Last Post: June 23rd, 2009, 05:03 AM
  2. [SOLVED] Fixing of bug for a small program
    By Koren3 in forum Threads
    Replies: 3
    Last Post: April 21st, 2009, 06:28 AM
  3. [SOLVED] Problem while getting a number from another thread
    By Koren3 in forum Threads
    Replies: 4
    Last Post: April 7th, 2009, 01:42 PM
  4. How to do thread communication in java
    By Koren3 in forum Threads
    Replies: 4
    Last Post: March 29th, 2009, 10:49 AM