• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

badLocationException problem catching exception

 
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am working on an ability to add text on top of existing text in a JTextPane, but as of now I am getting a badLocationException that needs to be caught or thrown.



I tried putting together a try catch block, but I seem to have things off.


Any suggestions on how toset this up correctly would be great.

Thanks a lot

 
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Quit multiposting questions. The reason you didn't get a response in your last posting was because the code you posted made no sense. We can't tell what you are doing based on a few random lines of code.

If you need further help then you need to create a SSCCE (Short, Self Contained, Compilable and Executable, Example Program), that demonstrates the incorrect behaviour.
 
Ranch Hand
Posts: 686
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ben Hultin wrote:I am working on an ability to add text on top of existing text in a JTextPane, but as of now I am getting a badLocationException that needs to be caught or thrown.



I tried putting together a try catch block, but I seem to have things off.


Any suggestions on how toset this up correctly would be great.

Thanks a lot



This is compile problem? You are not actually getting the exception, you are being told by the compiler you have to do this? I don't think you will get BadException inserting a string at position 0, but I guess you have to go through the motions of being ready to catch the exception anyway. Question: If you know you are inserting at position zero, why bother asking for the offset? and all the other stuff? That is what you are trying to do right> Find out what the bad location actually is?
 
Ben Hultin
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well the above code I mentioned was due to some articles I read up what needs to be done to catch this specific Exception.

As far as the position goes, I wasnt sure what to put there. I really want the text to appear at the current cursor point. (right now its appearing at the top).

I have compiled and zipped .jar file but it seems I cant attatch here, is there a better file format to send it that the forums will accept?

Thanks a lot
 
Fred Hamilton
Ranch Hand
Posts: 686
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ben Hultin wrote:well the above code I mentioned was due to some articles I read up what needs to be done to catch this specific Exception.

As far as the position goes, I wasnt sure what to put there. I really want the text to appear at the current cursor point. (right now its appearing at the top).

I have compiled and zipped .jar file but it seems I cant attatch here, is there a better file format to send it that the forums will accept?

Thanks a lot



best way to provide code is to paste it directly into the message within code tags, as you have already done.

There are a lot of issues that are coming in to play here. You talk about this specific exception, but there are things that all exceptions have in common, there doesn't have to be anything special about this particular exception.

I would sugest first learning a bit more about the basics of exception handling. Try a few very simple examples. Learn the bare minimum that is needed to catch this, and all exceptions. AT first don't do anything ecept catch the exception. Which means have no code in your catch block. Or maybe just as simple message that an exception has occurred. That will enable you to compile the code and experiment with the doc.insertString method. Then as you get a little more comfortable, you can actually have your catch bock do things, and it will make more sense to do this when you start inserting at a place other than 0.

chapter 80 and 81 at the following page does a decent job of explaining exceptions at a basic level. It should allow you to move forward with your project.

http://chortle.ccsu.edu/CS151/cs151java.html


p.s. again, just to clarify. I don't see how you can compile anything that has a document.insertString() command without also including a compileable try/catch block. And then once you have that, you need to clarify whether or not your are indeed getting an exception when you try to insert at position zero. My guess is, unless you do something obviously wrong, like specifying an insertion point that exceeds the length of the text the pane contains, you probably won't get the exception anyway.
 
reply
    Bookmark Topic Watch Topic
  • New Topic