ArrayList  
Author Message
Petar Popara





PostPosted: 2005-4-4 19:59:00 Top

java-programmer, ArrayList
Why this doesn't work:

ArrayList l = new ArrayList();
l.add(new String("aaa")));
String[] s = l.toArray();

JavaClassCast exception I got. What should I do?


 
Petar Popara





PostPosted: 2005-4-4 22:54:00 Top

java-programmer >> ArrayList
> String[] s = (String[])l.toArray(new String[0]);

I got java.lang.NullPointerException. :(


 
Petar Popara





PostPosted: 2005-4-4 23:02:00 Top

java-programmer >> ArrayList >
> I got java.lang.NullPointerException. :(

Not with String[], but with X509Certificate[]. It works fine with String[].


 
 
Gillmer J. Derge [TeamB]





PostPosted: 2005-4-4 23:04:00 Top

java-programmer >> ArrayList Petar Popara wrote:
>>I got java.lang.NullPointerException. :(
> Not with String[], but with X509Certificate[]. It works fine with String[].

If you actually got it from this code (or its equivalent with
X509Certificate):

String[] s = (String[])l.toArray(new String[0]);

then your list (l) is null. Otherwise, more likely, it happened
somewhere else. Print the stack trace and make sure of what line it's
pointing to as the original cause of the exception.

--
Gillmer J. Derge [TeamB]
 
 
Petar Popara





PostPosted: 2005-4-4 23:23:00 Top

java-programmer >> ArrayList
I think it is the best to post some code sample:

DBCertificate dbcert = new DBCertificate(a_cert);
ArrayList certs = new ArrayList();
ArrayList crls = new ArrayList();

try {
while (true) {
X509Certificate cert =
getX509CertFromBytes(dbAPI.getCert(dbcert.getIssuerDNDigest()));
certs.add(cert);

dbcert = new DBCertificate(cert);

if (dbcert.getIssuerDNDigest().equals(dbcert.getSubjectDNDigest()))
break;
}

ret.cert = (X509Certificate[])certs.toArray(new X509Certificate[0]);//
<--- exception

} catch ()

I've also tried to move cert variable out of while(), but it didn't helped.
while loop loops only once in my test case.


 
 
Kevin Dean [TeamB]





PostPosted: 2005-4-4 23:26:00 Top

java-programmer >> ArrayList Petar Popara wrote:

>
> Why this doesn't work:
>
> ArrayList l = new ArrayList();
> l.add(new String("aaa")));
> String[] s = l.toArray();
>
> JavaClassCast exception I got. What should I do?

The toArray() method returns Object[], not String[] (see the source
code of ArrayList for implementation details). If you want an array of
String, I suggest the following:

String[] s = (String[])l.toArray(new String[0]);

That will force ArrayList to build an array of the appropriate size of
type String[]. You then cast it back to the String[] type.

--
Kevin Dean [TeamB]
Dolphin Data Development Ltd.
http://www.datadevelopment.com/

NEW WHITEPAPERS
Team Development with JBuilder and Borland Enterprise Server
Securing Borland Enterprise Server
http://www.datadevelopment.com/papers/index.html

Please see Borland's newsgroup guidelines at
http://info.borland.com/newsgroups/guide.html
 
 
Gillmer J. Derge [TeamB]





PostPosted: 2005-4-4 23:37:00 Top

java-programmer >> ArrayList Petar Popara wrote:
> ret.cert = (X509Certificate[])certs.toArray(new X509Certificate[0]);//
> <--- exception

What is ret? Are you sure it isn't null?

--
Gillmer J. Derge [TeamB]
 
 
Gillmer J. Derge [TeamB]





PostPosted: 2005-4-5 9:23:00 Top

java-programmer >> ArrayList Kevin Dean [TeamB] wrote:
> Two possible causes for the NullPointerException:
>
> 1) ret is null
> 2) certs is null

But certs can't be null, because this line:

certs.add(cert);

succeeded earlier without throwing an exception, and none of the
subsequent code changes certs. I think it has to be ret.

--
Gillmer J. Derge [TeamB]
 
 
Kevin Dean [TeamB]





PostPosted: 2005-4-5 9:55:00 Top

java-programmer >> ArrayList Petar Popara wrote:

> ret.cert = (X509Certificate[])certs.toArray(new
> X509Certificate[0]);// <--- exception

Two possible causes for the NullPointerException:

1) ret is null
2) certs is null

--
Kevin Dean [TeamB]
Dolphin Data Development Ltd.
http://www.datadevelopment.com/

NEW WHITEPAPERS
Team Development with JBuilder and Borland Enterprise Server
Securing Borland Enterprise Server
http://www.datadevelopment.com/papers/index.html

Please see Borland's newsgroup guidelines at
http://info.borland.com/newsgroups/guide.html
 
 
Petar Popara





PostPosted: 2005-4-5 14:42:00 Top

java-programmer >> ArrayList
> What is ret? Are you sure it isn't null?

Yes, it was null. :( Thank you.


 
 
andrewtitus





PostPosted: 2006-3-3 5:41:00 Top

java-programmer >> ArrayList Iam writting a program that read a external file into an applet which uses
the names in the ArrayList to get images. The error I get is:

蟇ebApplet.java:62: cannot find symbol
舷symbol : method getImage(java.lang.Object)
舷location: class WebApplet
舷 coolImage[j] = getImage(names.get(j))

Someone pointed out that the types are incompatible but not sure how to get
names of pictures from ArrayList to my coolImage[]. Is there anyway to
convert this to be compatible or is my logic wrong.

for(int j=0; j<names.size();j++) {

coolImage[j] = getImage(names.get(j));
// coolImage[j] = getImage(getCodeBase(),names.get(j));
}

Thank You,
Andy
 
 
Hendrik Maryns





PostPosted: 2006-3-3 6:06:00 Top

java-programmer >> ArrayList -----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
NotDashEscaped: You need GnuPG to verify this message

email***@***.com uitte de volgende tekst op 03/02/2006 10:40 PM:
> Iam writting a program that read a external file into an applet which uses
> the names in the ArrayList to get images. The error I get is:
>
> 蟇ebApplet.java:62: cannot find symbol
> 舷symbol : method getImage(java.lang.Object)
> 舷location: class WebApplet
> 舷 coolImage[j] = getImage(names.get(j))
>
> Someone pointed out that the types are incompatible but not sure how to get
> names of pictures from ArrayList to my coolImage[]. Is there anyway to
> convert this to be compatible or is my logic wrong.
>
> for(int j=0; j<names.size();j++) {
>
> coolImage[j] = getImage(names.get(j));
> // coolImage[j] = getImage(getCodeBase(),names.get(j));
> }

We need more code. You probably need a cast to String, as it seems you
are using old style List: getImage((String)names.get(j)) or use generic
List<String>.

H.
--
Hendrik Maryns

==================
www.lieverleven.be
http://aouw.org
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (GNU/Linux)

iD8DBQFEB2xiiOEY3xKMFEERAt3UAJ9IcfKIB9Tkrod9fZIS+GhoSpuJFwCgqc/d
2fSTcLomXutDalfV4rZU25o=
=TAu8
-----END PGP SIGNATURE-----
 
 
andrewtitus





PostPosted: 2006-3-3 6:13:00 Top

java-programmer >> ArrayList here is my code.

import javax.swing.JApplet;
import java.awt.Event;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.util.*;
import java.awt.*;
import javax.swing.ImageIcon;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JButton;
import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;
import java.net.URL.*;


public class WebApplet extends JApplet
{
private JButton previous,next;
private JPanel buttonPanel;
ArrayList names = new ArrayList();
String[] temp;
Image coolImage[];
String url,pictureName;
Scanner fileScan, urlScan;
Font title,documentText;
int i = 0;


public void init()
{

previous = new JButton("Previous");
next = new JButton("Next");


ButtonListener listener = new ButtonListener();
previous.addActionListener(listener);
next.addActionListener(listener);
buttonPanel = new JPanel();

add(buttonPanel);

buttonPanel.add(previous);
buttonPanel.add(next);

try{
readFile();
}
catch (IOException ioex)
{
ioex.printStackTrace();
}

for(int j=0; j<names.size();j++) {

coolImage[j] = getImage(getCodeBase(),names.get(j)); // problem is
here I tried casting to string
}

//System.out.println(names);

} // end init()
public void readFile() throws IOException
{

fileScan = new Scanner (new File("pictures.txt"));


// Read and process each line of the file
while (fileScan.hasNext())
{

names.add(fileScan.nextLine());

}
// System.out.println (names);
}



public void paint(Graphics page){


super.paint(page);
setBackground(Color.black);


title = new Font("Jokerman",Font.BOLD,(int)(getHeight()*0.02) );
documentText = new Font("Courier New",Font.BOLD,(int)(getHeight()*0.02) );

//set color for text
page.setColor(Color.white);
page.setFont(title);


page.setFont(documentText);

page.drawImage(coolImage[i],0,30,this);


} //end paint()


private class ButtonListener implements ActionListener
{
public void actionPerformed (ActionEvent event)
{

if(event.getSource()==next) {
if(i==4){

repaint();
}else
i++;
repaint();
}
if(event.getSource()==previous) {
if(i==0){

repaint();
}else
i--;
repaint();
}

}

}


} //end webApplet


*****************
蟇ebApplet.java:62: cannot find symbol
舷symbol : method getImage(java.net.URL,java.lang.Object)
舷location: class WebApplet
舷 coolImage[j] = getImage(getCodeBase(),names.get(j));
 
 
andrewtitus





PostPosted: 2006-3-3 6:15:00 Top

java-programmer >> ArrayList If I cast to string

coolImage[j] = getImage(getCodeBase(),(String)names.get(j));

I get:

java.lang.NullPointerException
舷 at WebApplet.init(WebApplet.java:62)
舷 at sun.applet.AppletPanel.run(AppletPanel.java:373)
舷 at java.lang.Thread.run(Thread.java:595)
舷
 
 
Hendrik Maryns





PostPosted: 2006-3-3 6:21:00 Top

java-programmer >> ArrayList -----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
NotDashEscaped: You need GnuPG to verify this message

email***@***.com uitte de volgende tekst op 03/02/2006 11:12 PM:
> here is my code.

> String[] temp;
> Image coolImage[];

Please don?t mix idioms. I prefer Image[] coolImage, the intention is
clearer.


> *****************
> 蟇ebApplet.java:62: cannot find symbol
> 舷symbol : method getImage(java.net.URL,java.lang.Object)
> 舷location: class WebApplet
> 舷 coolImage[j] = getImage(getCodeBase(),names.get(j));

You don?t have a method getImage, and you don?t inherit one either.
Define it, or invoke it against the appropriate object. Learn Java.

H.
--
Hendrik Maryns

==================
www.lieverleven.be
http://aouw.org
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (GNU/Linux)

iD8DBQFEB2/diOEY3xKMFEERApReAKDcRdzXDSKT7Fl9TDvm6cZgldZ4MQCg1vOR
TsL6MGZi8dtLryaezofnad4=
=SbaH
-----END PGP SIGNATURE-----
 
 
klynn47





PostPosted: 2006-3-3 6:28:00 Top

java-programmer >> ArrayList The problem is that you never initialize the array coolImage.

 
 
klynn47





PostPosted: 2006-3-3 6:30:00 Top

java-programmer >> ArrayList The method getImage is inherited. The problem is that the poster is not
sending the correct parameters to the method.

 
 
andrewtitus





PostPosted: 2006-3-3 6:37:00 Top

java-programmer >> ArrayList Isnt the getImage() set up in the import java.awt.Image; import header. i
used it this way before in a program and it worked. I have this program
working with pictures hard coded into string array and the call with
getImage() worked. If all I did was switch to an ArrayList I should be able
to access indexs of array the same way butfor some reason not letting me.

Thank You,
Andy
 
 
andrewtitus





PostPosted: 2006-3-3 6:39:00 Top

java-programmer >> ArrayList I did not initalize it because I do not know length of array. Should I just
give a default size?

Thank You,
Andy
 
 
klynn47





PostPosted: 2006-3-3 7:20:00 Top

java-programmer >> ArrayList I would recommend that you use an ArrayList instead of an array to hold
the images.