Jump to content

Return Array

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
5 replies to this topic

#1
isuru

isuru

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 233 posts
I created this testing code. But I get nullPointerException, I may being a fool.
Can anyone clarify this for me.



package test;


import java.io.File;



public class Main {


   public long[] getFileSize()

    {

        File file = new File("D:\\new");


        String fileSize[] = file.list();

        long TotalSize[] = null;



        for(int i=0; i<fileSize.length;i++)

        {

            TotalSize[i] = file.getTotalSpace();


        }


        return TotalSize;


    }


    public static void main(String[] args) {

       Main app = new Main();

       app.getFileSize();

    }


}


Lost!

#2
Sinipull

Sinipull

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 386 posts
You are attempting to access a part of an array that doesn't exist yet.
right here:
TotalSize[i] = file.getTotalSpace();

TotalSize hasn't been initialized yet. Why are you trying to fill an array of longs with the numbers returned from getTotalSpace method is beyond my understanding...

#3
isuru

isuru

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 233 posts

Sinipull said:

You are attempting to access a part of an array that doesn't exist yet.
right here:
TotalSize[i] = file.getTotalSpace();

TotalSize hasn't been initialized yet. Why are you trying to fill an array of longs with the numbers returned from getTotalSpace method is beyond my understanding...

I need to get a list of arrays... but i think above code gets just numbers.... but i need get list of file last modified.
Lost!

#4
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts

isuru said:

I need to get a list of arrays... but i think above code gets just numbers....
A list of arrays is something like:
List<Class[]> listOfArrays .. Which doesn't make much sence here.

isuru said:

but i need get list of file last modified.
What does that has to do with the filesize?

#5
isuru

isuru

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 233 posts

oxano said:

A list of arrays is something like:
List<Class[]> listOfArrays .. Which doesn't make much sence here.


What does that has to do with the filesize?

I am creating file browser. like windows explorer. So I need to put file size in a table with file name...
Lost!

#6
Roman Y

Roman Y

    Programmer

  • Members
  • PipPipPipPip
  • 189 posts
Well I think that you get a null pointer exception becuase you do not specify the length of your array. infact it looks like that you only create a variable to array and point it to null. You should create one like this:
long[] totalSize = new long[size];
if you don't know exactly how long array is going to be you might want to create an arrayList that increases the array-size everytime it hits the borders...