Results 1 to 2 of 2
  1. #1
    Join Date
    Feb 2009
    Posts
    2
    Rep Power
    0

    Default Passing integer array from javascript to java

    Hi Everyone,

    I am having trouble passing an array between javascript and java.I am using DWR.

    Demo.getData(userId,f1,f2,f3,CreateTable);

    Demo is the java class I need to access and CreateTable is the fuction I need to pass my javascript arrays to.

    f1,f2,f3 are my javascript arrays.

    in Demo.java CreateTable is declared as :

    public List CreateTable(int userId,int[] f1,int[] f2,int[] f3)

    I need to pass the arrays of f1,f2 and f3 as Integer[] objects;
    This is how I am trying to this:

    Integer[] intObj = null;

    if(f1!=null){
    intObj = new Integer[f1.length];
    for(int a=0;a<f1.length;a++){
    intObj[a] = new Integer(filter[a]);
    }
    }

    I have to then pass this to a hashmap:

    HashMap<String,Integer[]> filters = new HashMap<String,Integer[]>();
    filters.put("tagIds", intObj);

    I get a java.lang.NullPointerException

    However this works:
    Integer[] tags = new Integer[]{1,2,3,4};
    filters.put("tagIds",tags);

    I am sure that the javascript array is being correctly passed to CreateTable()

    Can some one point out what I am doing wrong?

    Thanks,
    Lavanya

  2. #2
    Join Date
    Feb 2009
    Posts
    2
    Rep Power
    0

    Lightbulb Fixed!

    It turns out my problem was not with passing arrays between javascript and java.

    My objective was to convert int[]f1 to Integer[] f1 and pass this to another function.

    It was possible that in some cases int[] f1 would be empty,
    so I would directly pass 'null' the function.(intObj=null)

    However the function I was calling did not know how to handle 'null'.So, now whenever int[] f1 is empty I pass:
    Integer[] intObj = new Integer[]{}

    So I guess the whole problem boils down to the difference between:
    Integer[] intObj = new Integer[]{}
    and
    Integer[] intObj = null

Similar Threads

  1. How do you store a random integer in an array?
    By Unknown in forum New To Java
    Replies: 6
    Last Post: 03-21-2010, 10:40 PM
  2. how use string array while passing to a function
    By sks9s9 in forum New To Java
    Replies: 2
    Last Post: 02-03-2009, 12:44 PM
  3. array passing dynamically
    By jazz2k8 in forum Advanced Java
    Replies: 2
    Last Post: 10-16-2008, 10:29 PM
  4. Replies: 3
    Last Post: 08-24-2008, 01:51 PM
  5. passing an array into an instance
    By lockmac in forum New To Java
    Replies: 1
    Last Post: 08-08-2007, 09:35 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •