Can I get a 'better' verbose output in Netbeans 7.3? - java

I'm trying to find an output mode that will show me the execution and order of every method called in the running program. Verbose and debugger output detail don't seem to give me that. Is there any way to get a detailed output like the one I've described here? Thanks!

You can do something like this using the following ways:
One: Put either of these codes in every method in your program:
System.out.println(Thread.currentThread().getStackTrace()[1].getMethodName());
System.out.println(new Object(){}.getClass().getEnclosingMethod().getName());
Two: Use the dumpStack() method:
Thread.currentThread().dumpStack();
Three: Use the printStackTrace of Throwable
new Throwable().printStackTrace();
Four: This is a variation of the first solution
StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
for(StackTraceElement st : stackTrace){
System.err.println(st);
}

Related

How to run code example chat in chapter 2 ebook Java Message Service?

How can run chat.class in chapter 2 ebook Java Message Service?
code here
I can't run main method Although i add javax.jms still can't run
/* Run the Chat client */
public static void main(String [] args){
try{
if(args.length!=3)
System.out.println("Topic or username missing");
// args[0]=topicName; args[1]=username; args[2]=password
Chat chat = new Chat(args[0],args[1],args[2]);
// read from command line
BufferedReader commandLine = new
java.io.BufferedReader(new
InputStreamReader(System.in));
// loop until the word "exit" is typed
while(true){
String s = commandLine.readLine();
if(s.equalsIgnoreCase("exit")){
chat.close(); // close down connection
System.exit(0);// exit program
}else
chat.writeMessage(s);
}
}catch(Exception e){ e.printStackTrace(); }
}
}
error
Topic or username missing
java.lang.ArrayIndexOutOfBoundsException: 0
at chap2.chat.Chat.main(Chat.java:97)
Seems you're quite new to Java.
First, notice that the message you get ("Topic or username missing") comes from the code itself, so the code is running.
Look where this comes from: it is printed because args.length is not equal to 3. When you run a Java class from the command line, the parameters are passed into the args array. So, you should have provided enough parameters.
That should solve the direct problem. Below the line, I'd like to explain a bit more.
The error message
java.lang.ArrayIndexOutOfBoundsException: 0
at chap2.chat.Chat.main(Chat.java:97)
is quite clear: it goes wrong in line 97 of Chat.java. In the main method.
If you see this, you should look in your code for that line (most IDE's tell you the line number, if you know where to look).
When you post a question here on SO (or anywhere), and have a stack trace
with a line number, it is good practice to point out which line the line number refers to. That helps those who read your question to better understand the problem.
An ArrayIndexOutOfBoundsException means that you have an array index that is pointing outside the array.
I have a hunch that the line in question is this one:
Chat chat = new Chat(args[0],args[1],args[2]);
In this case the index value is 0, as we can see in the error message. It may be a bit confusing for a beginner, but args[0] is the first value in your array. (args[1] is the second, args[2] the third, and so on).
So if 0 is already out of the bounds of the array... that means your array doesn't even have a first value. It has 0 values.
The error then results from Java trying to read the first element of an array that has 0 elements. The solution is to:
either make sure that the array has enough values
or provide an error message and stop executing that part of the code.
This example is a bit sloppy because it did provide an error message, but then continued to execute the code until it broke.

How to get the stackTrace from an exception without printStackTrace?

Is it possible to get the whole trace from an exception without using printStackTrace()?.
One of the reasons it's because I use CheckStyle and I'm getting an ugly message. It's not enough just to call toString(). I want to get the complete message, because I needed to send me by email.
Currently, I'm doing
StringWriter e = new StringWriter();
exception.printStackTrace(new PrintWriter(e));
--Avoid tracing with printStackTrace(), use log4j instead.
Otherwise, I could disable CheckStyle there.
If you want to get error details try Apache Commons Lang api
There is a class ExceptionUtils . You can use getFullStackTrace
Have you tried?:
Throwable thr = e.getStackTrace();
StackTraceElement[] lines = thr.getStackTrace();
And working from there. Also, take a look at the javadocs Oli linked above
You can get the stack trace from the current thread,
check this :
Thread.currentThread().getStackTrace()
Try this, you will be able to print stack trace with no check style error.
import org.apache.commons.lang.exception.ExceptionUtils;
String errorMessage = ExceptionUtils.getFullStackTrace(exception);
logger.debug(errorMessage);

NullPointerException for Query

I got NullPointerException error after using toString().substring() . I need to use this method. Help me please. I am not very sure i can use that method inside the query.
Orginal
String selectItemMasDetl="select MMITNO,MMITDS,MMALUN,MMSPUN from"+DBConnect.SCHEMA+".mitmas where MMCONO=888 and MMITNO="+ItemNo+"'";
Use ToString().substring()
String selectItemMasDetl="select MMITNO,MMITDS,MMALUN,MMSPUN from"+DBConnect.SCHEMA+".mitmas where MMCONO=888 and MMITNO="+ItemNo.toString().substring(0,4)+"'";
error log
java.lang.NullPointerException
[12/13/11 16:24:28:594 SGT] a6cc007 SystemErr R at com.------erp.report.stocklotsalesreport.StockLotSalesReportGet.getItemMasDetl
ANS:
selectItmMas="select MMITNO,MMITDS,MMALUN,MMSPUN from "+DBConnect.SCHEMA+".mitmas where MMCONO=888 and MMITNO like '"+ salesRecordListTO.getItemNo().trim() +"%'";
I use like operator and pass parameter instead of using trim. Now i get the result i want.
Thank you so much guys.
It means that at least in some cases either ItemNo or, DBConnect or the returned value of the toString() (as suggested) is null. The problem is that you don't have a stack trace, and so you can't be sure where the error is thrown. It could be even on another line, for what we (don't) know now.
Try to wrap with a try catch the relevant lines and print a stack trace, check the line of code where is happening, and post here again. Here is how:
try {
//code throwing exception
} catch (Exception ex) {
ex.printStackTrace();
}
Side note: seems that you need a space after the from, also you're closing a single quote, but I don't see the opening. Below, for reference, your code, only reformatted, not fixed:
String selectItemMasDetl = "select MMITNO,MMITDS,MMALUN,MMSPUN from"
+ DBConnect.SCHEMA + ".mitmas where MMCONO=888 and MMITNO="
+ ItemNo.toString().substring(0,4) + "'";
NullPointerExeption means that ItemNo is null, so when trying to use ItemNo.toString(), it will be interpreted as null.toString() which will throw the exception.
A check on null is one possibility (the easiest), see answer of Stivlo.
Depending on how quick the query is a count query can be done also, if count ==0 don't bother to do the real query.
Oops, sorry, not reading good: the null exception is already in building the query string. Well maybe this is of use in your next nullpointer exception :)
StringBuilder might be a solution, but than I think you'll get an invalid querystring, just use Stivlo's answer.

Why Android doesn't print the stack trace?

Even explicitly writing e.printStackTrace() it doesn't print to the console, why?
Checkout Logcat in DDMS perspective it should be there not in the Console tab.
Or use the ddms command tool to read logcat.
printStackTrace() doesn't print to the console, it prints to the standard error stream. If you want to print to the screen, set your display text to e.getMessage() or e.getStackTrace(). (Although I do recommend learning to debug with logcat instead.)
use Log.X() where X is the type of error console you want(Log.e, Log.v, Log.d, etc.)
You cannot print to console on Android. You have to use Android Log class. When I have an exception and I want to print it I'm using:
for (StackTraceElement element : exception.getStackTrace())
Log.e("my_tag", element.toString());
or you can try
String stackTrace = Log.getStackTraceString(throwable);
And after that you can print it to Android Log.

can't find file... using eclipse and file/filereader/bufferedreader

http://pastebin.com/m5fa7685e
It seems to fail when getting f3.. Output is:
not ready
File is null
Exception in thread "main" java.lang.NullPointerException
at BuabFile.parseBUAB(BuabFile.java:93)
at AddressBook.createBrowseForm(AddressBook.java:232)
at AddressBook.(AddressBook.java:51)
at Main.main(Main.java:4)"
But not before then - no file not found errors or anything...
My guess would be that the parseBUAB() method receives a "null" argument. Which means that it could be that it is the AddressBook class is responsible for the error.
It looks like you forgot to assign a value to BuabFile.file static field. You may want to add this to the end of your readFile() method:
BuabFile.file = f3;
I am guessing your AddressBook.createBrowseForm method looks something like this:
String filename = ...;
BuabFile buab = new BuabFile(filename);
buab.readFile();
ArrayList<String> buabLines = buab.returnFile(); // Returns null because readFile() never assigned a value to BuabFile.file
ArrayList<Buab> buabList = buab.parseBUAB(buabLines);
From all I can see, you just call parseBUAB(..) with a null value. I can't see the call to that method so you have to check the rest of your code.
For your 'not ready' output, which is created because your BufferedReader f3 is 'not ready', the API says
True if the next read() is guaranteed not to block for input, false otherwise.
Maybe you just call it too fast and the file is not loaded yet. Play with Thread.sleep() before calling ready() on the stream. Maybe a some-milliseconds blocking is just normal for File I/O.
And third - if f3 is the BufferedReader you want to keep, you have to assign it to the member file in the readFile() method. But now that's all I found ;)
I'm confused further but have found an answer sort of - I'm using windows 7 and have tried it on a windows xp computer and the code compiles fine and reads in the file (other errors you lot have noted are to be changed anyway through development - this was just one stick in the way...).
I'm wondering if there is some Windows 7 error with eclipse and opening/reading files...

Categories

Resources