473,490 Members | 2,489 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How to return 2D array from a method in java?

itiger
12 New Member
I wrote sample code to test but it gives error. Following is the code that i wrote:

Expand|Select|Wrap|Line Numbers
  1. class ArrCreate
  2. {
  3.     public int[][] createArray()
  4.     {
  5.         int[][] s = new int[3][];
  6.         s[0] = new int[4];
  7.         s[1] = new int[2];
  8.         for(int i = 0; i < s.length; i++)
  9.         {
  10.             for(int j=0; j < s[i].length; j++)
  11.             {
  12.                 s[i][j] = j + 1;
  13.             }
  14.         }    
  15.         return s;
  16.     }
  17.     public void displayArray()
  18.     {
  19.         int[][] a;
  20.         a = createArray();
  21.         for(int i = 0; i < a.length; i++)
  22.         {
  23.             for(int j=0; j < a[i].length; j++)
  24.             {
  25.                 System.out.println(a[i][j]);
  26.             }
  27.             System.out.println();
  28.         }
  29.  
  30.     }
  31. }
  32.  
  33. class MultiArray
  34. {
  35.     public static void main(String args[])
  36.     {
  37.         ArrCreate Arr = new ArrCreate();
  38.         Arr.displayArray();
  39.     }
  40. }
  41.  
The program gives following error:
Exception in thread "main" java.lang.NullPointerException
at ArrCreate.createArray(MultiArray.java:10…
at ArrCreate.displayArray(MultiArray.java:2…
at MultiArray.main(MultiArray.java:37)
Jan 17 '11 #1
2 37569
horace1
1,510 Recognized Expert Top Contributor
you define s with 3 elements s[3] but only created s[0] and s[1]
Expand|Select|Wrap|Line Numbers
  1.         int[][] s = new int[3][];
  2.         s[0] = new int[4];
  3.         s[1] = new int[2];
  4.         s[2] = new int[2];    // << missing??
  5.  
Jan 17 '11 #2
itiger
12 New Member
Thank you, it is working now.
Jan 17 '11 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

0
6442
by: Marc van Boven | last post by:
I'm stuck with the following problem: My nusoap-client calls a server-function giveCombination(). The function giveCombination should return something like array( => array( 'a_id' => 6,...
16
2466
by: priya | last post by:
Hi all, I am new to this group.I am working in c language.I have dount in pointer? how to retun array of pointer in function? example main()
2
1676
by: Ali | last post by:
hi i need to send some data to the function and want to return array of object bac lik dim obj() as myclass=AddIt(i,j i dont know that is write syntax or no public function addit(byval i as...
7
11041
by: Oleg Konovalov | last post by:
Hi, I am trying to pass a bunch of checked checkboxes (Javascript array) from page1 to the Java action class on subsequent web page (page2). (on page 1 I have a bunch of DB rows with a checkbox,...
4
1960
by: ankurmaheshwari | last post by:
hii all How to make 2-d array in java when no of rows are not known let me know if u have the solution 4 it... Regards ankur maheshwari
10
5545
by: Raj | last post by:
I need a VB function to return array of collections like Private Type Employee empname as string address as string salary as integer deptno as integer End Type dim employees() as Employee
1
4811
by: MimiMi | last post by:
I'm trying to decrypt a byte array in java that was encrypted in C#. I don't get any error messages, just a result that's completely not what I was hoping for. I think I am using the same type of...
2
17935
by: MimiMi | last post by:
I'm trying to decrypt a byte array in java that was encrypted in C#. I don't get any error messages, just a result that's completely not what I was hoping for. I think I am using the same type of...
0
3536
by: Miraj Godha | last post by:
Hi , I want to send array from java program to postgre. For oracle we can achieve it by using: ArrayDescriptor desc = ArrayDescriptor.createDescriptor("TYPE", conn); ARRAY newArray = new...
0
7112
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6974
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7356
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5448
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4878
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4573
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3084
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1389
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
277
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.