/**
 * @(#)TestClassOne.java
 *
 *
 * @aziza meshal
 * @version 1.00 2011/11/12
 */

import java.util.Scanner;
public class TestClassOne {
public static void main(String [] args)
{ int size;
Scanner input=new Scanner (System.in);
	System.out.println("plz enter size of array");
	size =input.nextInt();
	  int [] a1 = new int [size];
		  ClassOne [] a2 =  new ClassOne [size];
       for(int j=0;j<a1.length;j++)
   {
   	  System.out.println(a1[j]);
   	  a2[j].print();
   }
		 }
		 	  	 public static  void fillArray(int  a1[] , ClassOne  a2[] ){
		  	 	Scanner input=new Scanner (System.in);
		  for(int i=0;i<a1.length;i++)
{
	a1[i]=input.nextInt();
  a2[i]= new ClassOne( a1[i],a1[i]*2);}}
		}

there is no error but when i run it appear this

Exception in thread "main" java.lang.NullPointerException
at TestClassOne.main(TestClassOne.java:23)

Process completed.

Recommended Answers

All 4 Replies

the out put should be like this
if size =4
a1={1,4,5,3}
a2={(1,10),(4,10),(5,10),(3,10)}
print will be
1,(1,10)
4, (4,10)
5,(5,10)
3,(3,10)

ClassOne [] a2 = new ClassOne ;
creates an array of references to ClassOne objects, but initially all those references are null. You must initialise each element with a reference to an actual ClassOne. On line 21 you try to call the print method for an uninitialsed array element - hence the NPE.
Perhaps you should call your fillArray method before trying to print from the array?

import java.util.Scanner;
public class TestClassOne {


public static void main(String [] args)
{ int size;
Scanner input=new Scanner (System.in);
	System.out.println("plz enter size of array");
	size =input.nextInt();
	  int [] a1 = new int [size];
		  ClassOne [] a2 =  new ClassOne [size];
		  fillArray(int[],ClassOne[] );
       for(int j=0;j<a1.length;j++)
   {
   	  System.out.println(a1[j]);
   	  a2[j].print();
   }
		 }

there error
C:\Users\usa\Desktop\New folder (2)\TestClassOne.java:20: '.class' expected
fillArray(int[],ClassOne[] );
^
C:\Users\usa\Desktop\New folder (2)\TestClassOne.java:20: '.class' expected
fillArray(int[],ClassOne[] );

i correct but its give me wrong output
this is first file

public class ClassOne {

     private int x;
    private int y;
    public ClassOne(){
    	x=1;
    	y=1;
    }
    public ClassOne(int z, int az){
    	int x=z;
    	int y=az;
    }
    public void print (){

    	System.out.println("( value of " +x+",value of y = "+y+")");}
}

second file

import java.util.Scanner;
public class TestClassOne {


public static void main(String [] args)
{ int size;
Scanner input=new Scanner (System.in);
	System.out.println("plz enter size of array");
	size =input.nextInt();
	  int [] a1 = new int [size];
		  ClassOne [] a2 =  new ClassOne [size];
		  fillArray(a1,a2 );
       for(int j=0;j<a1.length;j++)
   {
   	  System.out.print(a1[j]);
   	  a2[j].print();
   }
		 }
		 	  	 public static  void fillArray(int  a1[] , ClassOne  a2[] ){
		  	 	Scanner input=new Scanner (System.in);
		  for(int i=0;i<a1.length;i++)
{
	a1[i]=input.nextInt();
  a2[i]= new ClassOne( a1[i],a1[i] );}}
		}

wrong oputput
--------------------Configuration: <Default>--------------------
plz enter size of array
4
1
2
3
4
1( value of 0,value of y = 0)
2( value of 0,value of y = 0)
3( value of 0,value of y = 0)
4( value of 0,value of y = 0)

Process completed.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.