You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by wenm <wm...@tdc.dk> on 2008/06/02 11:34:38 UTC

problem when reading properties file

I want to read a properties file test.properties, and show the value which is
defined in properties file in a lable. But I can't read the properties file

I have tried 
property.load(ClassLoader.getSystemResourceAsStream("test.properties"));
property.load(this.getClass().getClassLoader().getResourceAsStream("test.properties"));
property.load(PackageResource.get(test.class,
"test.properties").getResourceStream().getInputStream());
property.load(((WebApplication)
Application.get()).getServletContext().getResourceAsStream("test.properties"));

But it always generate null point error
java.lang.NullPointerException    
 at java.util.Properties$LineReader.readLine(Properties.java:365)     
at java.util.Properties.load(Properties.java:293)

Then I tried to test reading in main method, and it works fine there. 
public static void main(String[] args){
          Properties property = new Properties();
          try {
           
property.load(ClassLoader.getSystemResourceAsStream("test.properties"));
                     } catch (IOException e) {
            e.printStackTrace(); }
          System.out.println(property.getProperty("a key"));
     } 

Really can't understand why. Did I do something in wrong way? Thanks for any
help or sugestion.
 
-- 
View this message in context: http://www.nabble.com/problem-when-reading-properties-file-tp17597421p17597421.html
Sent from the Wicket - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: problem when reading properties file

Posted by Eyal Golan <eg...@gmail.com>.
Exactly as James suggest and what I said earlier:
use something like this,
add( new Label( "page.label", new ResourceModel( "page.label" ) );
Instead of 'page.label' do something like error.label.key#

where key# is what you get from the service.



On Mon, Jun 2, 2008 at 3:05 PM, James Carman <ja...@carmanconsulting.com>
wrote:

> On Mon, Jun 2, 2008 at 8:01 AM, wenm <wm...@tdc.dk> wrote:
> >
> > Sure.
> >
> > Maybe I need to explain more explicitly.
> >
> > I will get the unique error code from web service if there is something
> > wrong. And then I would like to map the error codes to user-friendly
> > messages (which are in properties file), and show the messages
> conditionally
> > based on the error type in a label.
> >
> > For example
> > if(error code == 1){
> >  add(new Label("a", property.getProperty(key1)));
> > } else if (error code ==2){
> >  .............
> > }
> >
> > So the first thing is that I have to read the properties file and let the
> > property to load it, so I can get the value(the message which I want to
> > show).
>
> Have you looked at org.apache.wicket.model.ResourceModel?  You could do:
>
> new Label("a", new ResourceModel(key1))
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>


-- 
Eyal Golan
egolan74@gmail.com

Visit: http://jvdrums.sourceforge.net/
LinkedIn: http://www.linkedin.com/in/egolan74

Re: problem when reading properties file

Posted by James Carman <ja...@carmanconsulting.com>.
On Mon, Jun 2, 2008 at 8:10 AM, wenm <wm...@tdc.dk> wrote:
>
> yup, it works.
> But I would like to know the way to read the file, for studying and also
> maybe future use.

For future use, I'd suggest you look at the java.util.ResourceBundle
class.  That will probably do what you are looking for.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: problem when reading properties file

Posted by wenm <wm...@tdc.dk>.
yup, it works.
But I would like to know the way to read the file, for studying and also
maybe future use. 

jwcarman wrote:
> 
> 
> Have you looked at org.apache.wicket.model.ResourceModel?  You could do:
> 
> new Label("a", new ResourceModel(key1))
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
> 
> 
> 

-- 
View this message in context: http://www.nabble.com/problem-when-reading-properties-file-tp17597421p17599820.html
Sent from the Wicket - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: problem when reading properties file

Posted by James Carman <ja...@carmanconsulting.com>.
On Mon, Jun 2, 2008 at 8:01 AM, wenm <wm...@tdc.dk> wrote:
>
> Sure.
>
> Maybe I need to explain more explicitly.
>
> I will get the unique error code from web service if there is something
> wrong. And then I would like to map the error codes to user-friendly
> messages (which are in properties file), and show the messages conditionally
> based on the error type in a label.
>
> For example
> if(error code == 1){
>  add(new Label("a", property.getProperty(key1)));
> } else if (error code ==2){
>  .............
> }
>
> So the first thing is that I have to read the properties file and let the
> property to load it, so I can get the value(the message which I want to
> show).

Have you looked at org.apache.wicket.model.ResourceModel?  You could do:

new Label("a", new ResourceModel(key1))

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: problem when reading properties file

Posted by wenm <wm...@tdc.dk>.
Sure.

Maybe I need to explain more explicitly. 

I will get the unique error code from web service if there is something
wrong. And then I would like to map the error codes to user-friendly
messages (which are in properties file), and show the messages conditionally
based on the error type in a label.

For example
if(error code == 1){
  add(new Label("a", property.getProperty(key1)));
} else if (error code ==2){
  .............
}

So the first thing is that I have to read the properties file and let the
property to load it, so I can get the value(the message which I want to
show).

And problen is whatever I tried, it failed to read as what I wrote before 


Have we discussed *why* you're doing what you're doing?  Perhaps
you're approaching the problem the wrong way.  Care to share the
actual problem you're facing which caused you to choose this approach
with us?

-- 
View this message in context: http://www.nabble.com/problem-when-reading-properties-file-tp17597421p17599649.html
Sent from the Wicket - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: problem when reading properties file

Posted by James Carman <ja...@carmanconsulting.com>.
Have we discussed *why* you're doing what you're doing?  Perhaps
you're approaching the problem the wrong way.  Care to share the
actual problem you're facing which caused you to choose this approach
with us?

On Mon, Jun 2, 2008 at 7:51 AM, wenm <wm...@tdc.dk> wrote:
>
> :) The problem is that I have to get different massages from properties file,
> and then comes to  my original question.
>
>
> so the problem is that you can get different messages?
> if so, why not use isVisible() as Gabor suggested.
> And for the String itself, why not use a utility that converts the message
> to your liking?
>
> --
> View this message in context: http://www.nabble.com/problem-when-reading-properties-file-tp17597421p17599504.html
> Sent from the Wicket - User mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: problem when reading properties file

Posted by wenm <wm...@tdc.dk>.
lol :) I haven't get it so far, so I don't know how's the properties'
massage.

mis-typing, sorry. 

Johan Compagner wrote:
> 
>>
>> :) The problem is that I have to get different massages from properties
>> file,
>> and then comes to  my original question.
>>
> 
> hmm i tried Chinese, Thais and some others.
> But i never got a massage from a property file, how does that feel??
> 
> johan
> 
> 

-- 
View this message in context: http://www.nabble.com/problem-when-reading-properties-file-tp17597421p17599687.html
Sent from the Wicket - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: problem when reading properties file

Posted by Johan Compagner <jc...@gmail.com>.
>
> :) The problem is that I have to get different massages from properties
> file,
> and then comes to  my original question.
>

hmm i tried Chinese, Thais and some others.
But i never got a massage from a property file, how does that feel??

johan

Re: problem when reading properties file

Posted by wenm <wm...@tdc.dk>.
:) The problem is that I have to get different massages from properties file,
and then comes to  my original question.


so the problem is that you can get different messages?
if so, why not use isVisible() as Gabor suggested.
And for the String itself, why not use a utility that converts the message
to your liking?

-- 
View this message in context: http://www.nabble.com/problem-when-reading-properties-file-tp17597421p17599504.html
Sent from the Wicket - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: problem when reading properties file

Posted by Eyal Golan <eg...@gmail.com>.
so the problem is that you can get different messages?
if so, why not use isVisible() as Gabor suggested.
And for the String itself, why not use a utility that converts the message
to your liking?

On Mon, Jun 2, 2008 at 2:03 PM, wenm <wm...@tdc.dk> wrote:

>
> Thanks.
>
> But still the lable will show different messages according to different
> error situation. So even to make it show up conditionally can't solve the
> problem.
>
>
> Gabor Szokoli wrote:
> >
> > On 6/2/08, wenm <wm...@tdc.dk> wrote:
> >>  But my label only show the message when the web service returns error,
> >> not
> >>  constant showing. So it is not a good idea to use resourceModel.
> >
> > I know I'm not answering your original question, but until someone
> > does, you could look at either the FeedbackPanel which you might be
> > reimplementing, or at overriding the isVisible function of the label
> > to make it show up conditionally.
> >
> >
> > Gabor Szokoli
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> > For additional commands, e-mail: users-help@wicket.apache.org
> >
> >
> >
>
> --
> View this message in context:
> http://www.nabble.com/problem-when-reading-properties-file-tp17597421p17598771.html
> Sent from the Wicket - User mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>


-- 
Eyal Golan
egolan74@gmail.com

Visit: http://jvdrums.sourceforge.net/
LinkedIn: http://www.linkedin.com/in/egolan74

Re: problem when reading properties file

Posted by wenm <wm...@tdc.dk>.
Thanks. 

But still the lable will show different messages according to different
error situation. So even to make it show up conditionally can't solve the
problem.


Gabor Szokoli wrote:
> 
> On 6/2/08, wenm <wm...@tdc.dk> wrote:
>>  But my label only show the message when the web service returns error,
>> not
>>  constant showing. So it is not a good idea to use resourceModel.
> 
> I know I'm not answering your original question, but until someone
> does, you could look at either the FeedbackPanel which you might be
> reimplementing, or at overriding the isVisible function of the label
> to make it show up conditionally.
> 
> 
> Gabor Szokoli
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
> 
> 
> 

-- 
View this message in context: http://www.nabble.com/problem-when-reading-properties-file-tp17597421p17598771.html
Sent from the Wicket - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: problem when reading properties file

Posted by Gabor Szokoli <sz...@gmail.com>.
On 6/2/08, wenm <wm...@tdc.dk> wrote:
>  But my label only show the message when the web service returns error, not
>  constant showing. So it is not a good idea to use resourceModel.

I know I'm not answering your original question, but until someone
does, you could look at either the FeedbackPanel which you might be
reimplementing, or at overriding the isVisible function of the label
to make it show up conditionally.


Gabor Szokoli

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: problem when reading properties file

Posted by wenm <wm...@tdc.dk>.
Thanks Eyal. 

I have tried with resourceModel before, it works fine.

But my label only show the message when the web service returns error, not
constant showing. So it is not a good idea to use resourceModel. 
-- 
View this message in context: http://www.nabble.com/problem-when-reading-properties-file-tp17597421p17597687.html
Sent from the Wicket - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: problem when reading properties file

Posted by Eyal Golan <eg...@gmail.com>.
I'm not sure what's wrong, but why not use the i18n feature?
Create a .properties file with name of the page that your label is located,
put it in the same folder of the page (like were you put the html).
The label should be something like this:

add( new Label( "page.label", new ResourceModel( "page.label" ) );

check:
http://cwiki.apache.org/WICKET/general-i18n-in-wicket.html

Hope that was helpful.

Eyal



On Mon, Jun 2, 2008 at 12:34 PM, wenm <wm...@tdc.dk> wrote:

>
> I want to read a properties file test.properties, and show the value which
> is
> defined in properties file in a lable. But I can't read the properties file
>
> I have tried
> property.load(ClassLoader.getSystemResourceAsStream("test.properties"));
>
> property.load(this.getClass().getClassLoader().getResourceAsStream("test.properties"));
> property.load(PackageResource.get(test.class,
> "test.properties").getResourceStream().getInputStream());
> property.load(((WebApplication)
>
> Application.get()).getServletContext().getResourceAsStream("test.properties"));
>
> But it always generate null point error
> java.lang.NullPointerException
>  at java.util.Properties$LineReader.readLine(Properties.java:365)
> at java.util.Properties.load(Properties.java:293)
>
> Then I tried to test reading in main method, and it works fine there.
> public static void main(String[] args){
>          Properties property = new Properties();
>          try {
>
> property.load(ClassLoader.getSystemResourceAsStream("test.properties"));
>                     } catch (IOException e) {
>            e.printStackTrace(); }
>          System.out.println(property.getProperty("a key"));
>     }
>
> Really can't understand why. Did I do something in wrong way? Thanks for
> any
> help or sugestion.
>
> --
> View this message in context:
> http://www.nabble.com/problem-when-reading-properties-file-tp17597421p17597421.html
> Sent from the Wicket - User mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>


-- 
Eyal Golan
egolan74@gmail.com

Visit: http://jvdrums.sourceforge.net/
LinkedIn: http://www.linkedin.com/in/egolan74