Matthew Busse

Ranch Hand
+ Follow
since Sep 29, 2010
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
In last 30 days
0
Forums and Threads

Recent posts by Matthew Busse

Paul Clapham wrote:
As a general rule you should declare your variables at the highest level of the inheritance hierarchy which makes sense. Obviously declaring the "exactMatches" variable as an Object is too far up in the inheritance hierarchy -- you do want to treat it as a list of STRProfile objects -- but ArrayList is too far down. List is about right. The other way to say this general rule is "Program to the interface, not to the implementation".



Thank you very much for the responses. Would you mind explaining this point a little more? I see this in code quite frequently, and I've never fully understood while it's better to do
instead of


I don't understand why it's better to declare variables at the highest level of the inheritance hierarchy...

Any explanation would be greatly appreciated!

Thanks!
13 years ago
Hello Java Ranchers,

I have stylistic/design patterns question.

If class A needs to get an object from class B, and class B may or may not have that object, is it better to write a separate method that first checks if class B has that object, then gets it, or is it better to write one get method, and first check if the get result is null?

For example, in this case, I have a separate "has" method


I could also image doing something like


The second option only calls one method on the queryProfile object, is that going to be more efficient?

Thanks!
13 years ago
Great, thank you everyone for clearing up my misunderstanding!

13 years ago
Hello Java Ranchers,

I've recently run into a question that makes me wonder if I'm misunderstanding a fundamental concept in Java programming. In this program I wrote, I make a copy of an array list, and then remove some items from that copy, but it seems like it's removing those items from the original array list as well, is that right? If I make object 2 equal to object 1, then anything I do to object 2 also happens to object 1?

Here's the code:


When I run this code, after I remove an allele from refAllelesLocal, it is removed from the ArrayList referenceAlleles also, is that supposed to happen?

Thanks!
13 years ago
Thanks for your help. I wound up just digging in and parsing the html response by hand. It was a good learning experience.

Happy New Year!
Well, maybe I just don't know how to use HtmlUnit. Is there a simple way to make a post request? From the examples I found online, it seems like I have to get the first page, then look through the html to find the name of the form I want to submit, as well as the names of the fields within that form, then set the fields to the values I want and then submit it back to the website. Something like this:



That seems much more awkward than making a post request with HttpClient. That and the fact that trying to run the above code gives me a NoClassDefFound error, pointing at the WebClient, even after checking to make sure I have all the required dependencies...
Hello Java Geniuses,

I have a question about parsing html pages with Java. I want to use HtmlUnit to parse tables from a webpage that is a response to a POST request. But, I want to make that post request using HttpClient, because making a post request using HtmlUnit is a real pain.

Is it possible to somehow convert the HttpEntity that comes back from the HttpClient post request into an HtmlPage, maybe by going through an input stream?

Or is this question not even make sense?

I want to make a post request like this:


Then do something like this (I know this doesn't work, but is there some way to convert the HttpEntity into an HtmlPage?)


I need to automate a bunch of post requests, but I don't see an easy way to do that using the available HtmlUnit tools.

Is this possible, or do I just need to manually parse the html that comes back in the HttpEntity?

Thanks!

Paul Clapham wrote:That just means that the "filesToMerge" variable is null. You're getting distracted by the JFileChooser, which is working perfectly well. And so is your code, up to that point.



Great, thanks! I thought the sourceFile variable was null.
13 years ago
Hello Java Ranchers,

I'm trying to use a JFileChooser to select multiple files, either from the same directory or multiple directories. I have a fundamental misunderstanding of how to do this.

To select multiple files from the same directory, all I need to do is enable MultiSelection, which I've done. That works fine.

To select multiple files from different directories, I'm thinking I need to choose the files individually, then add them to an array of files, then pass that array to my other classes. This is where I'm running into problems, I think it has something to do with the File[] returned by the .getSelectedFiles method of the JFileChooser. I don't know what File[] is, the javadoc says it's "An abstract representation of file and directory pathnames." I'm not sure what to do with that.

This is what I tried first:


This gives me a run-time Null pointer exception, pointing to line 13. I can print out the pathname of the file, so it seems like there is a file there, but when I try to add that file to the array, it gives me the null pointer exception. I tried creating a new File from the File[], but that didn't work either. So now I'm stuck.

Does anyone know how to make this work, or am I entirely on the wrong track for selecting multiple files from different directories?

Thanks!
13 years ago

Tim Moores wrote:I like the HtmlUnit (or jWebUnit) library for interacting programmatically with a web site. It has various methods for accessing and extracting specific bits of an HTML page's content.



Great, HtmlUnit looks like it has a lot of potential.

My only hesitation is that the form request methods in HtmlUnit seem rather clunky, you have to look at the source page to get the form name and the name of the submit button, etc.

Am I correct in thinking HtmlUnit is built on HttpClient? Is it possible to use the post request methods from HttpClient and feed that response into HtmlUnit?

Thanks!
Hello Java Geniuses,

I'm using HTTPClient to access a website using a POST request. Does HTTPClient provide any tools for easily parsing the response? The "tutorials" on the Apache website were not much help, I had to search several other sites just the get the POST request working.

For example, I'm getting back a table like this:


Is there an easy way to convert that into an excel file or XML document or something?

Thanks!



Tim Moores wrote:Just to ask the obvious: you've ascertained that "(cell != null)" is true, in other words, that "cell" is not null? If so, what value does "sourceCellValue.getCellType()" have?



Aha! That was the problem. The blank cell is null, not Cell_Type_Blank. (Which leads me to wonder when Cell_Type_Blank comes into play. But that's a different question)
I added an else statement to handle the case when cell != null is false.

Thanks!

Here's the revised code, for the benefit of future readers:

Thanks for your help. Alas, that didn't work. I added a println, to check what's going on, it doesn't seem like the blank cell is being recognized as a string or number case, so I still don't understand why the default isn't catching it?

Thanks!

Hello,

I'm trying to use Apache POI to read an excel file, then copy it to a new file with some extra information added, specifically, information about the types of data in the excel sheet. It's almost working, but I'm having problems with blank cells, my program doesn't seem to recognize them. I tried adding a case (line 61) to specifically act on blank cells. And even if that doesn't work, I would expect default to catch anything else, but neither solution is able to recognize a blank cell and insert "unknown" into the HashMap. Any ideas?

Thanks!



Hello,

Thanks for your response. It turns out the file is fine. My mistake was assuming the other application took a .xlsx file, when it actually only takes .xls.

Thanks anyway!

13 years ago