|
I once saw world war III break out over the rights and wrongs of static code blocks between an ex-Smalltalk OO purist and an ex-C++ hacker. No quarter asked or given on either side.
It's like anything: give someone a hammer and they will find a way to hit themselves on the head with it.
|
|
|
|
|
The try{}catch{} mechanism is specifically for catching exceptions thrown within a block of code, i.e inside a method. Putting such a mechanism in a class has no meaning as it includes things like variable definitions, overloaded methods etc.
The best things in life are not things.
|
|
|
|
|
Thanks cedric and richrd.
you solve my problem.
thanks a lot....
|
|
|
|
|
The code you write inside a class are NOT "executable", they're just declarations like variables and methods. The try {} catch {} block is designed to trap errors in executable code. Since only methods contain executable code, try {} catch {} can be placed inside methods only.
|
|
|
|
|
Hello,
My application needs to directly read cookies data from cookies4.dat file that is a cookies firl for Opera browser.
When I load the cookies file of opera it gives me a lot of noise. Here's the <a href="http://img840.imageshack.us/img840/7509/63274022.png">screenshot</a>.
How can I extract(/parse) values for each cookie from that .dat file?
Thanks.
|
|
|
|
|
Just a brief guess - the file ends on *.dat.
For some strange circumstances a *.dat file is a binary file without any specific associated format.
so you've got to give that data some meaning.
You should take a look in the documentation. There you'll find some info on Opera Cookie File Format[^].
regards Torsten
I never finish anyth...
|
|
|
|
|
Thank you. Actually, the problem is that I don't know how to read(/parse/extract values from) that Opera Cookie File Format.
|
|
|
|
|
|
hello every body...
may ,, i want to ask ,how do know the amount of content in the database and display at textfield ?
sorry my language not good
|
|
|
|
|
What do you mean by "the amount of content"; the number of tables, records, columns, bytes etc?
The best things in life are not things.
|
|
|
|
|
kellyyy wrote: how do know the amount of content in the database and display at textfield ?
Steps
1. Learn basic java
2. Learn about GUI programming in java.
3. Learn basic database including SQL. This has nothing to do with steps 1/2.
4. Learn JDBC. This is an API in java. Do NOT attempt this before steps 1 and 3.
5. Put the above together to create a program that does what you want.
|
|
|
|
|
I am writing a java applet that shows FireFox cookies. Is it possible to open the cookies.sqlite file and read that database in Java?
I tried with org.sqlite.JDBC driver but without success. And got exception: file is encrypted or is not a database.
Is it necessary to decrypt the file first or get a permission?
I tried SqlJetDb and it gives exception "org.tmatesoft.sqljet.core.SqlJetException: NOTADB: error code is NOTADB"
Is there anything else do the task of showing FireFox5 cookies?
Thanks.
-- Modified Wednesday, June 29, 2011 8:07 PM
|
|
|
|
|
|
|
Hi,
I'm usually programming in .net but now I have to do a project in java. Nothing wrong with it, but Eclipse demands try/catch blocks on sortof every call which might pose a problem, even if I have error handling in place. How do I get rid of that? I just need a quick check for something, but it even prevents me to compile it.
private void doSomething()
{
Properties properties = readProperties(chooser.getSelectedFile().getCanonicalPath()); <--- compile error
}
private void readProperties(String location)
{
}
While the location of my errorhandling might be debatable, this is for a proof of concept app where I *know* the input is correct.
A good programmer is someone who always looks both ways before crossing a one-way street. (Doug Linder)
|
|
|
|
|
what's wrong? Exceptions need to be catched.
Add the try/catch of the quick fix and go for it. If your code is fine it wont disturb you.
regards Torsten
I never finish anyth...
|
|
|
|
|
While I do agree with you, I was wondering if I could "force" a compile and run it, as I know that there won't be an exception on that line.
A good programmer is someone who always looks both ways before crossing a one-way street. (Doug Linder)
|
|
|
|
|
Java - Compiler - Building
uncheck "Abort build when build problems errors occure"
might "help". Still it remains to be really bad style.
regards Torsten
I never finish anyth...
|
|
|
|
|
This is caused by the declaration of the method you are calling. It includes a throws part in the statement. If you don't wish to catch the exception, because you have error handling somewhere else in the application, then you can ignore the exception by adding a throws statement to your method.
private void doSomething() throws IOException
{
Properties properties = readProperties(chooser.getSelectedFile().getCanonicalPath());
}
Please note this will only move the catch requirement up the call tree, meaning to whereever you are calling the doSomething method from. Which could be usefull when you have some type of generic exception handling in your code.
|
|
|
|
|
Thanks for your answer. This was what I've been looking for.
A good programmer is someone who always looks both ways before crossing a one-way street. (Doug Linder)
|
|
|
|
|
...so you don't have code assist? this is also provided as quick tip.
regards Torsten
I never finish anyth...
|
|
|
|
|
I do have code assist, but for some reason all I got was a compile error, not a hint on how to disable "possible exceptions". I must admit I'm not using the standard Eclipse, but an Eclipse version which is tailored for the Sonic ESB, so maybe there's something in there which prevents it.
And the tips are appreciated, as always.
A good programmer is someone who always looks both ways before crossing a one-way street. (Doug Linder)
|
|
|
|
|
Further to other answers, if a catch-able exception is thrown it must be caught in java. Their are two options, try-catch or use throws so that the calling method knows it could receive the exception and must act accordingly.
If it is a runtime error, say out of memory, then it does not need to be handled as you cannot predict how it will occur and build time.
Panic, Chaos, Destruction. My work here is done.
Drink. Get drunk. Fall over - P O'H
OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre
I cannot live by bread alone. Bacon and ketchup are needed as well. - Trollslayer
Have a bit more patience with newbies. Of course some of them act dumb - they're often *students*, for heaven's sake - Terry Pratchett
|
|
|
|
|
Hi I am having some trouble drawing on a canvas. The code below is a class that makes a small rect of 87x90 pixels and puts it on a panel. The purpose is to display a "tile" that has 8 "lights" that count down. I make several instances of this class that all run at the same time.
When input is received it contains the x and y position and it talks to an instance of this class according to the x and y position.
I instance the class like this:
tile = new TileMaker[9][9];
and:
tile[i][j] = new TileMaker(Integer.toString(tp.topo[i][j]),i,j);
i and j represent x and y.
When running the application I get input from devices with coordinates x and y, which call the ledcountdown1 in the instance of tile[x][y].
My problem is when the animation is running on several instances at the same time. Lets say the instance tile[0][0] is half way counting down and the instance tile[1][1] is called to start its count down, the first instance often resets and follows the count down of the new instance.
Sometimes a previously finished countdown shows 8 lights when a new instance is asked to count down.
I tried to make use of double buffering to fix this bug and at first it seemed to work fine but now I'm getting those annoying effects as before, though it is not as frequent as before.
I hope I make any sense to you guys.. And I apologize for the length of the message..
Any suggestions/comments are greatly appreciated.
Thank you in advance.
public class TileMaker extends JPanel{
private static final long serialVersionUID = 1L;
public static JPanel panel;
public static JLabel label;
public TileCanvas Drawingarea;
public TileMaker(String name, int xpos, int ypos){
}
public JPanel TileCreator(String name){
panel = new JPanel(new BorderLayout());
panel.setName(name);
panel.setPreferredSize(new Dimension(87, 90));
panel.setVisible(true);
Drawingarea = new TileCanvas();
panel.add(Drawingarea,BorderLayout.CENTER);
return panel;
}
}
class TileCanvas extends Canvas {
private static final long serialVersionUID = 345057978247188915L;
public static ArrayList<TileLeds> ledlist;
static String imageFile = "cleartile.jpg";
Image image;
public Timer ledcountdowntimer;
private Timer ledcountdowntimer1;
public int counter = 0;
public boolean isrunning = false;
public boolean gameover = false;
public int myspeed;
private int bwidth;
private int bheight;
private Image bimage;
private Graphics bgraphics;
public TileCanvas() {
super();
ledlist = new ArrayList<TileLeds>();
ledlist.add(new TileLeds(29,18,10,10,Color.BLACK));
ledlist.add(new TileLeds(47,18,10,10,Color.BLACK));
ledlist.add(new TileLeds(60,30,10,10,Color.BLACK));
ledlist.add(new TileLeds(60,48,10,10,Color.BLACK));
ledlist.add(new TileLeds(47,60,10,10,Color.BLACK));
ledlist.add(new TileLeds(29,60,10,10,Color.BLACK));
ledlist.add(new TileLeds(16,48,10,10,Color.BLACK));
ledlist.add(new TileLeds(16,30,10,10,Color.BLACK));
MediaTracker media = new MediaTracker(this);
image = Toolkit.getDefaultToolkit().getImage(imageFile);
media.addImage(image, 0);
try {
media.waitForID(0);
}
catch (Exception e){
}
}
public void ledcountdown1(int speed, final Color color, String command){
if(command == "run"){
if(isrunning == false){
myspeed = speed;
counter = 9;
isrunning = true;
ledcountdowntimer = new Timer();
ledcountdowntimer.schedule(new TimerTask(){
public void run(){
counter--;
for(int g=0; g<8; g++){
ledlist.get(g).setcolor(Color.BLACK);
repaint();
}
if(counter==0){
for(int g=0; g<8; g++){
ledlist.get(g).setcolor(Color.BLACK);
repaint();
}
isrunning = false;
ledcountdowntimer.cancel();
}
else{
for(int n=0;n<counter;n++){
ledlist.get(n).setcolor(color);
repaint();
}
}
}
},0,speed*10);
}
}
public void update(Graphics g){
paint(g);
}
public void paint(Graphics g){
resetBuffer();
if(bgraphics!=null){
bgraphics.clearRect(0,0,bwidth,bheight);
paintBuffer(bgraphics);
g.drawImage(bimage,0,0,this);
}
}
private void resetBuffer(){
bwidth=getSize().width;
bheight=getSize().height;
if(bgraphics!=null){
bgraphics.dispose();
bgraphics=null;
}
if(bimage!=null){
bimage.flush();
bimage=null;
}
System.gc();
bimage = createImage(bwidth, bheight);
bgraphics = bimage.getGraphics();
}
public void paintBuffer(Graphics g){
g.drawImage(image, 0, 0 , this);
for(int i=0; i<8; i++){
if(ledlist.get(i).getcolor()!=Color.BLACK){
g.setColor(ledlist.get(i).getcolor());
g.fillOval(ledlist.get(i).getx1(), ledlist.get(i).gety1(), ledlist.get(i).getx2(), ledlist.get(i).gety2());
}
}
}
}
modified on Wednesday, June 29, 2011 6:43 AM
|
|
|
|
|
Sounds like you're using the same instance of Timer multiple times.
- limit the scope of "ledcountdowntimer" to the method "ledcountdown"
- if possible instantiate it anonymous.
- in doubt: Extend the Timer (ledcountdowntimer) and give it a ID (UUID?) to prevent multi use.
regards Torsten
I never finish anyth...
|
|
|
|
|