Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7797349
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T23:39:21+00:00 2026-06-01T23:39:21+00:00

I have this Java class in a Jar file included as a dependency of

  • 0

I have this Java class in a Jar file included as a dependency of an Scala program (like the Axis jar):

class MyClass {
    private String[] someStrings;
    public String[] getSomeStrings() { return someStrings; }
}

In my Scala program I have a Java API that return an instance of MyClass instance of MyClass to my program in Scala:

val myInstance = JavaAPI.getInstanceOfMyClass()

Then I try to use the someStrings array in my Scala program but it’s null (let say that it wasn’t properly initialized)

for(str <- myInstance.getSomeStrings()) ...

So this throws a NullPointerException.

I’ve found that in order to use it in the for comprehension I can wrap it into an Option so that it handles the NPE properly.

for(str <- Option[Array[String]](myInstance.getSomeStrings).getOrElse(Array[String]())

But that doesn’t look OK to me.

Is there a way to create like an implicit method that takes that value even if it’s null and wrap it into the Option, like:

implicit def wrapNull(a: Null): Option[Nothing] = None
implicit def wrapArray(a: Array[String]): Option[Array[String]] = Some(a)

So that when I do:

for(str <- myInstance.getSomeStrings())

I don’t get the NPE

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T23:39:23+00:00Added an answer on June 1, 2026 at 11:39 pm

    I don’t think your version with getOrElse is that bad (you can make it a little shorter by removing the [Array[String]] after Option, since that can be inferred). If you want something even more concise, the following works:

    for (str <- Option(myInstance.getSomeStrings).flatten) ...
    

    You could also use the fact that Option has a foreach:

    for {
      strings <- Option(myInstance.getSomeStrings)
      str <- strings
    } ...
    

    Note that you can’t use yield here, for the reason that drexin highlights in a comment below.

    Or you could pimp MyClass:

    implicit def withNullWrapper(c: MyClass) = new {
      def getSomeStringsOrNot() = Option(c.getSomeStrings).getOrElse(Array[String]())
    }
    
    for (str <- myInstance.getSomeStringsOrNot) ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this class public class wordObject implements java.io.Serializable { String wordName; int occCount;
I have this class called PollFrame that extends JFrame in a file called PollFrame.java
I have a file in .gz format. The java class for reading this file
I want to have immutable Java objects like this (strongly simplified): class Immutable {
I have a jar file that runs this code: public class InputOutput { /**
I have an application which resides in ROOT. This application has a java class(in
In web.xml I have this <session-config> <session-timeout>2</session-timeout> </session-config> <listener> <listener-class>myapplication.SessionListener</listener-class> </listener> In the SessionListener.java
I have 2 files: xxxxxTest.java [ refer this ] public class xxxxxTest extends TestCase
I'm a Java-beginner, so please bear with this one. I have a class: class
I have this java string: String bla = <my:string>invalid_content</my:string>; How can I replace the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.